Leetcode - Two Sum III - Data structure design
https://leetcode.com/problems/two-sum-iii-data-structure-design/
Design and implement a TwoSum class. It should support the following operations: add
and find
.
add
- Add the number to an internal data structure.
find
- Find if there exists any pair of numbers which sum is equal to the value.
Example 1:
add(1); add(3); add(5); find(4) -> true find(7) -> false
type TwoSum struct { nums []int } /** Initialize your data structure here. */ func Constructor() TwoSum { return TwoSum{ nums: []int{}, } } /** Add the number to an internal data structure.. */ func (this *TwoSum) Add(number int) { this.nums = append(this.nums, number) } /** Find if there exists any pair of numbers which sum is equal to the value. */ func (this *TwoSum) Find(value int) bool { hash := map[int]bool{} for _, n := range this.nums{ if hash[value-n] { return true } hash[n] = true } return false } /** * Your TwoSum object will be instantiated and called as such: * obj := Constructor(); * obj.Add(number); * param_2 := obj.Find(value); */
Sep 25, 2021 08:31:49 PM
The red blossom Cheek will attract your mind.