1 class TwoSum {
 2 private:
 3     unordered_map<int, int> record;
 4 public:
 5     void add(int number) {
 6         record[number]++;
 7     }
 8 
 9     bool find(int value) {
10         if (record.size() == 0) return false;
11         for (unordered_map<int, int>::iterator it = record.begin(); it != record.end(); it++) {
12             if (it->first == (value - it->first)) {
13                 if (it->second > 1) {
14                     return true;
15                 }
16             } else if (record.find(value - it->first) != record.end()) {
17                 return true;
18             }
19         }
20         return false;
21     }
22 };

 

posted on 2015-03-24 17:24  keepshuatishuati  阅读(127)  评论(0)    收藏  举报