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;
}
}
};