-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.py
More file actions
26 lines (23 loc) · 789 Bytes
/
Copy pathTwoSum.py
File metadata and controls
26 lines (23 loc) · 789 Bytes
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
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
all_dict = {} #key : (count,index)
ind= 0
for n in nums:
if n in all_dict.keys():
all_dict[n] = (all_dict[n][0]+1,ind)
else:
all_dict[n] = (1,ind)
ind+=1
ans = []
c=0
for n in nums:
if (target-n) in all_dict.keys():
if target-n == n:
if all_dict[target-n][0] >1:
ans = [c,all_dict[target-n][1]]
break
else:
ans = [c,all_dict[target-n][1]]
break
c+=1
return ans