-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum_1.py
More file actions
29 lines (28 loc) · 735 Bytes
/
TwoSum_1.py
File metadata and controls
29 lines (28 loc) · 735 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
27
28
29
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
s = sorted(nums)
l = 0
r = len(s) - 1
while l < r:
t = s[r] + s[l]
if t == target:
al = s[l]
ar = s[r]
break
elif t > target:
r -= 1
if l < r and s[r] == s[r + 1]:
r -= 1
else:
l += 1
if l < r and s[l] == s[l - 1]:
l += 1
l = nums.index(al)
nums.reverse()
r = len(nums) - 1 - nums.index(ar)
return sorted([l, r])