解法1
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dit = {}
for i in range(len(nums)):
other = target - nums[i]
if other not in dit.keys():
dit[nums[i]] = i
else:
return dit[other], i
解法2
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dit = {}
for i in range(len(nums)):
dit[nums[i]] = i
for i in range(len(nums)):
other = target - nums[i]
if other in dit.keys() and dit[other] != i:
return dit[other], i
解法3
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)-1):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return i,j