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 };
LeetCode – Refresh – Two Sum III
原文:http://www.cnblogs.com/shuashuashua/p/4363452.html