Two Sum Solutions in C++
Number 1
Difficulty Easy
Acceptance 45.6%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/two-sum/// Author : Hao Chen// Date : 2014-06-17class Solution {public:/** The easy solution is O(n^2) run-time complexity.* ```* foreach(item1 in array) {* foreach(item2 in array){* if (item1 + item2 == target) {* return result* }* }* ```** We can see the nested loop just for searching,* So, we can use a hashmap to reduce the searching time complexity from O(n) to O(1)* (the map's `key` is the number, the `value` is the position)** But be careful, if there are duplication numbers in array,* how the map store the positions for all of same numbers?**///// The implementation as below is bit tricky. but not difficult to understand//// 1) Traverse the array one by one// 2) just put the `target - num[i]`(not `num[i]`) into the map// so, when we checking the next num[i], if we found it is exisited in the map.// which means we found the second one.//vector<int> twoSum(vector<int> &numbers, int target) {unordered_map<int, int> m;vector<int> result;for(int i=0; i<numbers.size(); i++){// not found the second oneif (m.find(numbers[i])==m.end() ) {// store the first one poisition into the second one's keym[target - numbers[i]] = i;}else {// found the second oneresult.push_back(m[numbers[i]]+1);result.push_back(i+1);break;}}return result;}// we also can store nums[i] into map, and find target - nums[i]vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int, int> m;vector<int> result;for (int i=0; i<nums.size(); i++) {if ( m.find(target - nums[i]) == m.end() ) {m[nums[i]] = i;}else{result.push_back(m[target - nums[i]]);result.push_back(i);}}return result;}};
C++ solution by pezy/LeetCode
#include <vector>using std::vector;#include <unordered_map>using std::unordered_map;class Solution {public:vector<int> twoSum(vector<int>& nums, int target) {std::unordered_map<int, int> record;for (int i = 0; i != nums.size(); ++i) {auto found = record.find(nums[i]);if (found != record.end())return {found->second, i};record.emplace(target - nums[i], i);}return {-1, -1};}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/two-sum/description//// Author : liuyubobobo/// Time : 2017-11-15#include <iostream>#include <vector>using namespace std;/// Brute Force/// Time Complexity: O(n^2)/// Space Complexity: O(1)class Solution {public:vector<int> twoSum(vector<int>& nums, int target) {for(int i = 0 ; i < nums.size() ; i ++)for(int j = i + 1 ; j < nums.size() ; j ++)if(nums[i] + nums[j] == target)return {i, j};throw invalid_argument("the input has no solution");}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {vector<int> nums = {0,4,3,5};int target = 10;print_vec(Solution().twoSum(nums, target));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/two-sum/description//// Author : liuyubobobo/// Time : 2017-11-15#include <iostream>#include <vector>#include <cassert>#include <unordered_map>using namespace std;/// Two-Pass Hash Table/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int,int> record;for(int i = 0 ; i < nums.size() ; i ++)record[nums[i]] = i;for(int i = 0 ; i < nums.size() ; i ++){unordered_map<int,int>::iterator iter = record.find(target - nums[i]);if(iter != record.end() && iter->second != i)return {i, iter->second};}throw invalid_argument("the input has no solution");}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {vector<int> nums = {0,4,3,0};int target = 0;print_vec(Solution().twoSum(nums, target));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/two-sum/description//// Author : liuyubobobo/// Time : 2017-11-15#include <iostream>#include <vector>#include <cassert>#include <unordered_map>using namespace std;/// One-Pass Hash Table/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int,int> record;for(int i = 0 ; i < nums.size() ; i ++){int complement = target - nums[i];if(record.find(complement) != record.end()){int res[] = {i, record[complement]};return vector<int>(res, res + 2);}record[nums[i]] = i;}throw invalid_argument("the input has no solution");}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {const int nums[] = {0,4,3,0};vector<int> nums_vec( nums, nums + sizeof(nums)/sizeof(int) );int target = 0;print_vec(Solution().twoSum(nums_vec, target));return 0;}