提交 c5ee87cc 编写于 作者: M MarineJoker

添加 167.两数之和 python代码

上级 372b92bf
......@@ -252,3 +252,19 @@ class Solution:
# 退出循环,则链表有结束,不可能为环形
return False
```
[MarineJoker](https://github.com/MarineJoker) 提供 167.两数之和 II - 输入有序数组 Python 代码
```python
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
left, right = 0, len(numbers) - 1
while left < right:
two_sum = numbers[left] + numbers[right]
if two_sum > target:
right -= 1 # 使得two_sum变小
elif two_sum < target:
left += 1 # 使得two_sum变大
elif two_sum == target:
return [left+1, right+1] # 由于索引由1开始
return [-1, -1]
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册