我刚刚开始在一些leetcode项目上工作,并且遇到以下问题。
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
我使用了以下代码:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in nums:
for x in nums:
if i + x == target:
index1 = nums.index(x)
index2 = nums.index(i)
break
answer = []
answer.append(index1)
answer.append(index2)
return answer
为什么我的解决方案失败了?