您的位置:首页 > 编程语言 > C语言/C++

leetcode 日经贴,Cpp code -Two Sum

2015-04-17 00:07 274 查看
Two Sum

class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
unordered_map<int,int> ump;
vector<int> ans;
for (int i = 0; i < numbers.size(); ++i) {
int other = target - numbers[i];
if (ump.find(other) != ump.end()) {
ans.push_back(ump[other]);
ans.push_back(i + 1);
} else {
ump[numbers[i]] = i + 1;
}
}
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: