Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5394f24

Browse files
Two Sum II - Input array is sorted
1 parent 123ae67 commit 5394f24

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

‎167-two-sum-ii-input-array-is-sorted.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
Output: [1,2]
1717
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
1818
"""
19-
class Solution(object):
20-
def twoSum(self,numbers, target):
21-
n = {}
22-
for index,num in enumerate(numbers):
23-
temp = target - num
24-
if temp in n:
25-
return [n[temp]+1,index+1]
26-
n[num] = index
19+
class Solution:
20+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
21+
start, end = 0, len(numbers) - 1
22+
23+
while start < end:
24+
cur_sum = numbers[start] + numbers[end]
25+
26+
if cur_sum == target:
27+
return [start + 1, end + 1]
28+
elif cur_sum < target:
29+
start += 1
30+
else:
31+
end -= 1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /