-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.py
More file actions
33 lines (29 loc) · 907 Bytes
/
Solution.py
File metadata and controls
33 lines (29 loc) · 907 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
30
31
32
33
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[nt]
"""
sorted_num = sorted(nums)
# two points
left = 0
right = len(nums) - 1
res = []
while (left < right):
sum = sorted_num[left] + sorted_num[right]
if sum == target:
# find out index
break;
elif sum > target:
right -= 1
else:
left += 1
if left == right:
return -1, -1
else:
pos1 = nums.index(sorted_num[left]) + 1
pos2 = nums.index(sorted_num[right]) + 1
if pos1 == pos2: # find again
pos2 = nums[pos1:].index(sorted_num[right]) + pos1 + 1
return min(pos1, pos2), max(pos1, pos2)