Leetcode No.1 Two Sum
-Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
给定一个数组,和一个target,求这个数组中 和为target的两个数的位置。
代码块
代码块语法遵循标准markdown代码,例如:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
int n = (int)nums.size();
for (int i = 0; i < n; i++) {
auto p = map.find(target-nums[i]);
if (p!=map.end()) {
return {p->second+1, i+1};
}
map[nums[i]]=i;
}
}
};