Skip to content

Commit f33a26f

Browse files
committed
committed from zkp
1 parent 0ccc833 commit f33a26f

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

LeetCode/TwoSum_1.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def twoSum(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
s = sorted(nums)
9+
l = 0
10+
r = len(s) - 1
11+
12+
while l < r:
13+
t = s[r] + s[l]
14+
if t == target:
15+
al = s[l]
16+
ar = s[r]
17+
break
18+
elif t > target:
19+
r -= 1
20+
if l < r and s[r] == s[r + 1]:
21+
r -= 1
22+
else:
23+
l += 1
24+
if l < r and s[l] == s[l - 1]:
25+
l += 1
26+
l = nums.index(al)
27+
nums.reverse()
28+
r = len(nums) - 1 - nums.index(ar)
29+
return sorted([l, r])

0 commit comments

Comments
 (0)