遍历数组,将数组中的元素和索引分别做为 unordered_map 的键和值,若是目标值和当前元素的差已经存于在map 的键中,即找到结果。python
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> tabel;
vector<int> index;
int n = nums.size();
for (int i = 0; i < n; i++)
{
int val = target - nums[i];
if (tabel.count(val) == 1)
{
index.push_back(tabel[val]);
index.push_back(i);
return index;
}
else
{
tabel[nums[i]] = i;
}
}
}
};
复制代码
遍历数组,将数组中的元素和索引分别做为字典的键和值,若是目标值和当前元素的差已经存于在字典的键中,即找到结果。数组
class Solution:
def twoSum(self, nums, target):
""" :type nums: List[int] :type target: int :rtype: List[int] """
hash_table = {}
for index, value in enumerate(nums):
num = target - value
if num in hash_table:
return [hash_table[num], index]
hash_table[value] = index
复制代码
获取更多精彩,请关注「seniusen」! ui