Leetcode170. Two Sum III - Data structure design
Solution1: HashMap
class TwoSum {
private HashMap<Integer, Integer> map;
/** Initialize your data structure here. */
public TwoSum() {
map = new HashMap<>();
}
/** Add the number to an internal data structure.. */
public void add(int number) {
if(!map.containsKey(number)){
map.put(number, 1);
}else{
map.put(number, map.get(number) + 1);
}
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value) {
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
int num1 = entry.getKey();
int num2 = value - entry.getKey();
if((num1 == num2 && map.get(num1) >1) || (num1 != num2 && map.containsKey(num2)))
return true;
}
return false;
}
}
/**
* Your TwoSum object will be instantiated and called as such:
* TwoSum obj = new TwoSum();
* obj.add(number);
* boolean param_2 = obj.find(value);
*/
Solution2: List + HashMap
class TwoSum {
private HashMap<Integer, Integer> map;
private List<Integer> list;
/** Initialize your data structure here. */
public TwoSum() {
map = new HashMap<>();
list = new ArrayList<>();
}
/** Add the number to an internal data structure.. */
public void add(int number) {
if(!map.containsKey(number)){
map.put(number, 1);
list.add(number);
}else{
map.put(number, map.get(number) + 1);
}
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value) {
for(int i = 0; i < list.size(); ++i){
int num1 = list.get(i);
int num2 = value - num1;
if((num1 == num2 && map.get(num1)>1) || (num1 != num2 && map.containsKey(num2))){
return true;
}
}
return false;
}
}
/**
* Your TwoSum object will be instantiated and called as such:
* TwoSum obj = new TwoSum();
* obj.add(number);
* boolean param_2 = obj.find(value);
*/