-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.Two_Sum.py
60 lines (52 loc) · 1.69 KB
/
1.Two_Sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution(object):
@staticmethod
def twoSum(nums, target):
h = {}
for i, num in enumerate(nums):
if (target - num) in h:
return [i, h[target - num]]
h[num] = i #这条语句不能放在if之前,不然会引发错误
@staticmethod
def combinationSum4(nums, target):
temp = nums[:]
temp.sort()
i = 0
j = len(temp) - 1
sum = 0
while i < j:
sum = temp[i] + temp[j]
if sum > target:
j -= 1
elif sum < target:
i += 1
else:
if temp[i] == temp[j]:
index = nums.index(temp[i])
index += 1
return(nums.index(temp[i]), nums[index:].index(temp[j]) + index)
return (nums.index(temp[i]), nums.index(temp[j]))
@staticmethod
def twoSum2(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hashTable = {}
for i in range(len(nums)):
hashTable[nums[i]] = i
for i in range(len(nums)):
if target - nums[i] in hashTable and i != hashTable[target - nums[i]]:
return [i, hashTable[target - nums[i]]]#这里必须是i,而不能是hashTable[nums[i]],那样的话当target为同一数值的元素相加而成的话,其结果是错的
return []
if __name__ == "__main__":
nums = [2, 7, 11, 15]
target = 9
print Solution.combinationSum4(nums, target)
nums = [0, 2, 4, 0]
target = 0
print Solution.twoSum(nums, target)
h = {}
h['h'] = 8
h['h'] = 10
print h