My AC Solution


  • 0
    K
    LinkedHashMap<Integer,Integer> numMap;
        
    public void TwoSum() {
        numMap = new LinkedHashMap<>();
    }
        
    /** Add the number to an internal data structure.. */
    public void add(int number) {
        numMap.put(number, numMap.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 num:numMap.keySet()){
            if (numMap.containsKey(value-num) && (value/2 != num || ( value /2 == num && numMap.get(num) > 1))) return true;
        }
        return false;
    }
    

    use linkedHashMap can avoid TLE


Log in to reply
 

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