class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
num_list = []
for i in range(len(nums)):
for j in nums:
if nums[i] + j == target and i != nums.index(j):
if len(num_list) < 2:
num_list.append(nums[i])
num_list.append(j)
index = 0
nums_ind = []
for j in nums:
if nums[index] in num_list:
nums_ind.append(index)
index+=1
return nums_ind

之後可以試著少用迴圈
試想:
利用 n = target – num 的方法
例如:
[2,7,11,15], 9 – 2 = 7, 找7
[3,2,4], 6 – 2 = 4, 找4