-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_TwoSum.py
39 lines (33 loc) · 955 Bytes
/
1_TwoSum.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
# https://leetcode.com/problems/two-sum/
# 2021 11.20
def twoSum(nums: list[int], target: int) -> list[int]:
# v1 3644ms
# len1=len(nums)
# for i in range(0,len1):
# num1=nums.pop(0)
# for idx,item in enumerate(nums):
# if num1+item==target:
# return [i, idx+i+1]
# v2
# len1=len(nums)
# for i in range(0,len1):
# for j in range(i+1,len1):
# if nums[i]+nums[j]==target:
# return [i, j]
# v3 copied 70ms
hashmap={}
for i in range(len(nums)):
hashmap[nums[i]]=i
complement=target-nums[i]
if complement in hashmap:
return[i,hashmap[complement]]
# 2022年05月20日 11:03:41
# using hash table 70ms
d={}
for idx, item in enumerate(nums):
if item not in d:
d[target-item]=idx
else:
return [d[item],idx]
a=[1,2,3,5,8,6,5]
print(twoSum(a,8))