LeetCode1
作者:互联网
解法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
标签:target,nums,int,LeetCode1,List,other,dit 来源: https://www.cnblogs.com/gmbjzg/p/14615811.html