leetcode刷题two sum 1

题目
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]
        """
        a1 = 0
        a2 = 0
        
        for i in range(len(nums)):
            for j in range((i+1),len(nums)):
                           if target - nums[i] == nums[j]:
                              a1 = i
                              a2 = j
                              break
            if a2 != 0:
                break
        return (a1,a2)

猜你喜欢

转载自blog.csdn.net/Docterzzz/article/details/78283761