Leetcode

给定 nums = [2, 7, 11, 15], target = 9

由于 nums[0] + nums[1] = 2 + 7 = 9
因此返回 [0, 1]

 

class Solution(object):
    def twoSum(self, nums, target):
         for i in nums:
             tmp = target -i

             if tmp!=i and tmp in nums:
                 a=[]
                 a.append(nums.index(i))
                 a.append(nums.index(tmp))
                 return a

             elif tmp in nums:
                 if nums.count(tmp)>1:
                     del_index = nums.index(i)
                     del nums[del_index]
                     a = []
                     a.append(del_index)
                     a.append(nums.index(tmp)+1)
                     return a
             else:
                 continue