[leetcode]Two Sum III - Data structure design

用个hashtable记录下

 

class TwoSum {
public:
    void add(int number) {
        rec[number]++;
    }

    bool find(int value) {
        for (auto it = rec.begin(); it != rec.end(); it++) {
            int val = value - it->first;
            if (val == it->first) {
                if (it -> second > 1) return true;
            } else {
                if (rec.find(val) != rec.end()) {
                    return true;
                }
            }
        }
        return false;
    }
private:
    unordered_map<int, int> rec;
};

 

你可能感兴趣的:(LeetCode)