Created
July 18, 2022 13:07
-
-
Save icameling/6605350026b33d4d25f8ddffadf627cb to your computer and use it in GitHub Desktop.
#哈希表 #两数之和
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) { | |
unordered_map<int, int> nums_map; | |
for (int i = 0; i < nums.size(); ++i){ | |
int k = target - nums[i]; | |
auto iter = nums_map.find(k); | |
if (iter != nums_map.end()) { | |
return {i, nums_map[k]}; | |
} | |
nums_map.insert(pair<int, int>(nums[i], i)); | |
} | |
return {}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment