-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path52.py
More file actions
25 lines (21 loc) · 655 Bytes
/
52.py
File metadata and controls
25 lines (21 loc) · 655 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
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
idx = {}
res = None
for i in range(len(nums)):
x = idx.get(nums[i], None)
if x is None:
idx[nums[i]] = [i]
else:
idx[nums[i]] = x + [i]
sorted_nums = sorted(nums)
for nb in sorted_nums:
sub = target - nb
x = idx.get(sub, None)
if x is not None:
if sub == nb and len(x) == 2:
res = x
break
res = [x[0], idx[nb][0]]
break
return res