class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> two;
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); i++) {
m.insert(pair<int, int>(nums[i], i));
}
unordered_map<int, int>::iterator it;
for (int i = 0; i < nums.size(); i++) {
if ((it = m.find(target - nums[i])) != m.end() && it->second != i) {
int first = min(i, it->second);
int second = max(i, it->second);
two.push_back(first);
two.push_back(second);
return two;
}
}
}
};
LetCode 1.两数之和
最新推荐文章于 2023-02-24 19:40:16 发布