-
方法一
Class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n=len(nums)
d={}
for i in range(n):
a = target - nums[i]
if nums[i] in d:
return d[nums[i]],i
else:
d[a]=i -
方法二
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n=len(nums)
for i in range(0,n):
a = target - nums[i]
if a in nums:
j = nums.index(a)
if i == j:
continue
else:
return i,j
break
else:
continue -
方法三
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n=len(nums)
for i in range(0,n):
for j in range(i+1,n):
if target-nums[i]==nums[j]:
return i,j
break
else:
continue
Leetcode 1. Two SUM(两数之和)Python
最新推荐文章于 2022-11-06 16:42:55 发布