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.
Initialization: Create a dictionary counts to store the frequency of each added number.
Adding Numbers: When a number is added using the add method, increment its count in the counts dictionary. If the number is not already in the dictionary, initialize its count to 1.
Finding a Pair:
The find method checks whether a pair of numbers exists that sums up to the specified value.
Iterate through the keys of the counts dictionary.
For each key (number), calculate the complement (target) that adds up to the specified value (target = value - num).
Check if the complement exists in the counts dictionary.
If the complement is the same as the current number, ensure that its count is at least 2 to satisfy the condition (i.e., the pair must have two distinct instances of the same number).
Return True if such a pair is found, otherwise continue the search.
If no pairs are found after iterating through the dictionary, return False.
classTwoSum:
def __init__(self):
self.counts = {}
defadd(self, number: int) ->None:
if number in self.counts:
self.counts[number] +=1else:
self.counts[number] =1deffind(self, value: int) -> bool:
for num in self.counts:
target = value - num
if target in self.counts:
if num == target and self.counts[target] <2:
continuereturn true
return false
classTwoSum {
private Map<Integer,Boolean> map;
private List<Integer> list;
privateint low = Integer.MAX_VALUE;
privateint high = Integer.MIN_VALUE;
/** Initialize your data structure here. */publicTwoSum() {
map =new HashMap<>();
list =new LinkedList<>();
}
/** Add the number to an internal data structure.. */publicvoidadd(int number) {
if(map.containsKey(number)){
map.put(number,true);
}else{
map.put(number,false);
list.add(number);
low = Math.min(low,number);
high = Math.max(high,number);
}
}
/** Find if there exists any pair of numbers which sum is equal to the value. */publicbooleanfind(int value) {
if(value < 2* low || value > 2*high) returnfalse;
for(int num : list){
int target = value - num;
if(map.containsKey(target)){
if(num != target) returntrue;
elseif(map.get(target)) returntrue;
}
}
returnfalse;
}
}
classTwoSum:
def __init__(self):
""" Initialize your data structure here. """ self.map = {}
self.list = []
self.low = float('inf')
self.high = float('-inf')
defadd(self, number: int) ->None:
""" Add the number to an internal data structure. """if number in self.map:
self.map[number] =Trueelse:
self.map[number] =False self.list.append(number)
self.low = min(self.low, number)
self.high = max(self.high, number)
deffind(self, value: int) -> bool:
""" Find if there exists any pair of numbers which sum is equal to the value. """if value <2* self.low or value >2* self.high:
returnFalsefor num in self.list:
target = value - num
if target in self.map:
if num != target:
returnTrueelif self.map[target]:
returnTruereturnFalse