1.两数之和-leetcode01
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
hashtable = dict()
for i, num in enumerate(nums):
if target - num in hashtable:
return [hashtable[target - num], i]
hashtable[nums[i]] = i
return []
"""
# 遍历列表
for i in range(len(nums)):
# 计算需要找到的下一个目标的数字
res = target - nums[i]
# 遍历剩下的元素,查找是否存在该数字
if res in nums[i+1:]:
return [i, nums[i+1:].index(res)+i+1]