Two Sum III - Data structure design Solutions in C++
Number 170
Difficulty Easy
Acceptance 33.6%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/two-sum-iii-data-structure-design/// Author : Hao Chen// Date : 2014-12-29class TwoSum {private:unordered_map<int, int> nums;public://O(1) addvoid add(int number) {nums[number]++;}//O(n) findbool find(int value) {int one, two;for(auto it = nums.begin(); it != nums.end(); it++){one = it->first;two = value - one;if ( (one == two && it->second > 1) ||(one != two && nums.find(two) != nums.end() ) ){return true;}}return false;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/two-sum-iii-data-structure-design//// Author : liuyubobobo/// Time : 2016-12-03#include <iostream>#include <unordered_map>#include <cassert>#include <stdexcept>using namespace std;/// Using HashMap/// Time Complexity: add: O(1) , find: O(n)/// Space Complexity: O(n)class TwoSum {private:// The numbers store the number pair represent (number, count of the number)unordered_map<int,int> numbers;public:// Add the number to an internal data structure.void add(int number) {numbers[number] += 1;}// Find if there exists any pair of numbers which sum is equal to the value.bool find(int value) {for( unordered_map<int,int>::iterator iter = numbers.begin() ; iter != numbers.end() ; iter ++ ){int num = iter->first;if( numbers.find(value - num) != numbers.end() ){if( value - num == num && numbers[num] == 1 )continue;return true;}}return false;}};int main() {TwoSum twoSum;twoSum.add(1);twoSum.add(3);twoSum.add(5);cout << twoSum.find(4) << endl;cout << twoSum.find(7) << endl;return 0;}