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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章