Skip to content

Commit

Permalink
双指针解法
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Oct 1, 2018
1 parent cc1e9c2 commit 4b6f432
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lc167. Two Sum II - Input array is sorted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
left = 0
right = len(numbers) - 1
while left < right:
tem = numbers[left] + numbers[right]
if tem > target:
right -= 1
elif tem < target:
left += 1
else:
return [left + 1, right + 1]


nums = [2, 7, 11, 15]
target = 9
s = Solution()
print(s.twoSum(nums, target))

0 comments on commit 4b6f432

Please sign in to comment.