【发布时间】:2021-09-07 19:01:53
【问题描述】:
我试图返回列表中两个元素的索引,其总和等于目标值(leetcode 问题):
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in nums :
for j in nums:
if nums.index(i)!=nums.index(j) and i+j==target:
return nums.index(i),nums.index(j)
Your input:
[2,7,11,15]
9
Output:
[0,1]
Expected:
[0,1]
对于给定的输入,上面的 sn-p 工作正常,但对于 [3,3] 它返回“[]”
Input:
[3,3]
6
Output:
[]
Expected:
[0,1]
我尝试使用简单的 for 循环,而不仅仅是用于 [3,3] 的所有相同输入,它返回 [] 例如:[1,1],[2,2][1,1,1] 等等 为什么它返回 [ ]?
【问题讨论】: