题目描述
解法1、切片后查找
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in nums:
j = target - i
start_index = nums.index(i)
next_index = start_index + 1
temp_nums = nums[next_index:]
if j in temp_nums:
return [nums.index(i), next_index + temp_nums.index(j)]
解法2、hash字典
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dict={}
for i in range(len(nums)):
if target-nums[i] not in dict:
dict[nums[i]]=i
else:
return[dict[target-nums[i]],i]