题目:实现两个整数相加,并返回元素位置。
说明:给定一个整数数组 nums
和一个目标值 target
,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
编程环境:python3
class Solution:
def twoSum(self, nums, target):
c=[]
i=0
while i<len(nums):
b=target-nums[i]
if b in nums:
if i==nums.index(b):
i+=1
else:
c.append(i)
c.append(nums.index(b))
i=1+i+nums.index(b)
else:
i+=1
return c
执行用时:1192ms
内存消耗:14.8M