C++ 3 lines hash table solution


  • 0
    class TwoSum {
    public:
        unordered_map<int,int>m;
    
        /** Add the number to an internal data structure.. */
        void add(int number) {
            m[number]++;
        }
        
        /** Find if there exists any pair of numbers which sum is equal to the value. */
        bool find(int value) {
            for(auto c:m) if(m.count(value-c.first)&&(value!=c.first*2||c.second>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.