class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dict = {}
for i in xrange(len(nums)):
x = nums[i]
if target-x in dict:
return (dict[target-x], i)
dict[x] = i
注:
最精華的思想是:
x = nums[i] dict[x] = i
取出第i個數字(以0為開頭),把它的值裝載進dict中成為key,而原來的序號i變成了value
最終就能夠輕松的取出,value的值,即數列中的序號。