Easy understand C++ solution


  • 1
    X
    class TwoSum {
        vector<int> nums;
        unordered_map<int, int> dict;
    public:
    
        void add(int number) {
            dict[number] = nums.size();
            nums.push_back(number);
        }
    
        bool find(int value) {
            for (int i = 0; i < nums.size(); i++) {
                int gap = value - nums[i];
                if (dict.find(gap) != dict.end() && i != dict[gap])
                    return true;
            }
            return false;
        }
    };

Log in to reply
 

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