From c5ee87cc7c84c804b3a776065b149fe46563c321 Mon Sep 17 00:00:00 2001 From: MarineJoker Date: Mon, 16 Nov 2020 23:39:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20167.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=20python=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\207\351\222\210\346\212\200\345\267\247.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" index f5edbd4..69ba5b0 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" @@ -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 -- GitLab