One minute
Leetcode 170
170. Two Sum III - Data structure design
I have to admit that this is not the best solution to solve this problem, but I think it is the easiest to understand. For all we do is:
- Append to
this.arr
toAdd(number)
. - And use a two-pointer approach for
Find(value)
.
type TwoSum struct {
arr []int
}
/** Initialize your data structure here. */
func Constructor() TwoSum {
return TwoSum{[]int{}}
}
/** Add the number to an internal data structure.. */
func (this *TwoSum) Add(number int) {
this.arr = append(this.arr, number)
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
func (this *TwoSum) Find(value int) bool {
sort.Ints(this.arr)
left, right := 0, len(this.arr) - 1
for left < right {
if this.arr[left] + this.arr[right] < value {
left++
} else if this.arr[left] + this.arr[right] > value {
right--
} else {
return true
}
}
return false
}
Read other posts