篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 167.两个Sum II - 输入数组是sorted.py相关的知识,希望对你有一定的参考价值。
# Time: O(n)
# Space: O(1)
# 167. Two Sum II - Input array is sorted
# Edge case
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
l, r = 0, len(numbers)-1
while l < r:
s = numbers[l] + numbers[r]
if s == target:
return [l+1, r+1]
elif s < target:
l += 1
else:
r -= 1
以上是关于python 167.两个Sum II - 输入数组是sorted.py的主要内容,如果未能解决你的问题,请参考以下文章