Leetcode 分享筆記

1. Two Sum

題目

leetcode
b

python


class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        # 第一輪:第一個數字進去,後面一個一個加,如果等於目標物,即停止,否則,進行第二輪(第二個數字)
        i = 0
        while(i < len(nums)):
            for j in range(i, len(nums)):
                total = 0
                if (j == len(nums) - 1):
                    total = total + 0
                else:
                    total = nums[i] + nums[j+1]
                if(total == target):
                    return [i, j+1]
            i = i + 1