未验证 提交 5771430a 编写于 作者: B BruceCat 提交者: GitHub

【167.两数之和】 【python】

【167.两数之和】 【python】
......@@ -279,3 +279,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.
先完成此消息的编辑!
想要评论请 注册