Python | Leetcode Python题解之第167题两数之和II-输入有序数组

题目:

题解:

python 复制代码
class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        low, high = 0, len(numbers) - 1
        while low < high:
            total = numbers[low] + numbers[high]
            if total == target:
                return [low + 1, high + 1]
            elif total < target:
                low += 1
            else:
                high -= 1

        return [-1, -1]
相关推荐
文丑颜不良啊3 小时前
LeetCode3281~3300题目解析
java·leetcode
kyle~5 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享6 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
梯度下降者6 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
文丑颜不良啊7 小时前
LeetCode3261~3280题目解析
java·leetcode
MSTcheng.7 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人8 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__8 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
Rain的Java大神之路8 小时前
LeetCode 40 组合总和 II:面试官想听的不是回溯,而是那行去重代码
java·leetcode·面试
用户0332126663679 小时前
使用 Python 轻松移除 Excel 文档中的重复行
python·excel