[leetcode]Two Sum III - Data structure design
Posted on 2014-12-26 10:23 1957 阅读(587) 评论(0) 编辑 收藏 举报用个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; };
by 1957