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].
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        if len(nums) <2:
            if nums[0] != target:
                return "no this"
            return [0]
        else:
            nums_new = nums
            for i in range(len(nums_new)):
                if target-nums_new[i] in nums_new[i+1:]:
                    return [i,nums_new.index(target-nums_new[i],i+1)]
            return False
        
result=Solution().twoSum([14,3,5,6,3],6)        
print(result)

相关文章:

  • 2022-12-23
  • 2021-04-11
  • 2022-02-27
  • 2021-12-12
  • 2021-10-04
  • 2021-10-23
  • 2021-12-06
  • 2021-06-25
猜你喜欢
  • 2021-06-08
  • 2021-07-23
  • 2022-02-15
  • 2021-11-11
  • 2021-04-25
  • 2021-12-22
  • 2022-12-23
相关资源
相似解决方案