很早之前开始学python,但是也只是停留在基础的层面,所以刷leetcode来记录一下自己犯的错误
题目如下:
主要的思路是:使用一个for循环来遍历整个list,得出如果成立的值,然后使用in查找是否在list 中,若存在,再利用python的list.index( )函数来确定索引,最后判断j是否等于i用来满足同样的元素不能被重复利用的要求
class Solution:
def twoSum(self,nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n = len(nums)
for i in range(n):
temp = target - nums[i]
if temp in nums:
j = nums.index(temp)
if j != i:
return i,j
break