问题描述
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.
example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
mycode
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
remain = target - nums[i]
if remain in nums[i+1:]:
if remain == nums[i]:
lst = []
for j in range(len(nums)):
if nums[j] == remain:
lst.append(j)
return [i,lst[1]]
else:
return [i,nums.index(remain)]
return False
Runtime: 1120 ms
Memory Usage: 14.7 MB
捂脸哭