题目
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, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
思路1
根据冒泡排序思想,不断遍历数组取两者的和比较
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
if len(nums) < 2:
return []
for x in range(len(nums)-1):
for y in range(x+1, len(nums)):
if nums[x] + nums[y] == target:
return [x, y]
return []
思路二
遍历一遍数组,取值时计算满足相加所需的另一个值当做key存入dict,value为本值的index,取值时用当前值为key在dict中查找,如果存在则为相加得到target的另一个数的index
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
if len(nums) < 2:
return []
buf_dict = {}
for i in range(len(nums)):
if nums[i] in buf_dict: # 如果满足相加为target条件的值存在
return [buf_dict[nums[i]], i]
else:
buf_dict[target - nums[i]] = i # 保存下之前数值所在的位置,key为相加为target的所需值
return []