cpp solution accepted


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