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]
相关推荐
码云骑士4 小时前
09-Python模块导入机制-sys.path与循环导入的死锁式排查
开发语言·python
暖阳华笺4 小时前
【数据结构与算法】哈希专题
数据结构·c++·算法·leetcode·哈希算法
天佑木枫4 小时前
第5天:循环 —— 让程序重复执行
python
聆风吟º4 小时前
【Python编程日志】Python基础数据类型完整梳理
开发语言·python·数据类型
盼小辉丶4 小时前
OpenCV-Python实战(28)——OpenCV计算摄影从HDR图像融合到全景拼接
python·opencv·计算机视觉
AKA__Zas4 小时前
芝士算法(滑动窗口片 2.0)
java·算法·leetcode·学习方法
shchojj4 小时前
ChatGPT Prompt Engineering for Developers - Expanding
开发语言·python·prompt
俊俊谢4 小时前
【python】FastAPI 实时推送:从 SSE 到 WebSocket
python·websocket·fastapi
四代水门4 小时前
LeetCode刷算法题(C++)
c++·算法·leetcode
stephon_1005 小时前
Agent 接入 MCP 后上下文爆炸、工具选串?一种“按需激活“的工具加载方案(含实现)
人工智能·python·ai