Simple Accepted Java soln


  • 0
    R
    public class TwoSum {
            private Map<Integer, Integer> map = null;
            /** Initialize your data structure here. */
            public TwoSum() {
                map = new HashMap<>();
            }
    
            /** Add the number to an internal data structure.. */
            public void add(int number) {
                map.put(number, map.getOrDefault(number, 0)+1);
            }
    
            /** Find if there exists any pair of numbers which sum is equal to the value. */
            public boolean find(int value) {
                for(Integer key : map.keySet()){
                    int target = value - key;
                    if(map.containsKey(target)){
                        if(target != key || (map.get(key) > 1)){
                            return true;
                        }
                    }
                }
                return false;
            }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.