發表文章

目前顯示的是 7月, 2019的文章

[Python] LeeCode 1. Two Sum

Language Information : Python3 Description : Two Sum URL :  https://leetcode.com/problems/two-sum/ Code : class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: arrayList = [] for i in range(len(nums)): subSum = target - nums[i] for x in range(i,len(nums)): if x==i: continue elif subSum == nums[x]: arrayList.append(i) arrayList.append(x) return arrayList break else: pass subSum = 0 - 宣告一個 arrayList  = []去存結果, 使用最外層的 for loop 巡 nums 這個 input list, 宣告參數 subSum 讓 target 減去正在被巡的for element, 使用內層 for loop 再次巡 nums 並找出是否還有相減後相符的數字, 找到後將其位置 append 至 arrayList, 若未找到則將 subSum 歸零,重新繼續尋找。