Last active
November 5, 2019 08:59
LeetCode OJ:1.Two Sum. FYI: https://yunlinsong.blogspot.com/2019/04/leetcodetwo-sum.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> twoSum(vector<int>& nums, int target) { | |
multimap<int, int> nTable; | |
multimap<int, int>::iterator it, cur; | |
vector<int> result; | |
for(int i = 0; i < nums.size(); i++) | |
nTable.insert(make_pair(nums[i], i)); | |
for(cur = nTable.begin(); cur != nTable.end(); cur++) { | |
int c = target - cur->first; | |
it = nTable.find(c); | |
if(it != nTable.end() && cur->second != it->second) { | |
if(it->first == cur->first) { | |
result.push_back(it->second); | |
result.push_back(cur->second); | |
return result; | |
} | |
result.push_back(cur->second); | |
result.push_back(it->second); | |
return result; | |
} | |
} | |
return result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment