当前位置:网站首页>LeetCode167: Sum of two numbers in sorted array
LeetCode167: Sum of two numbers in sorted array
2022-07-30 16:55:00 【Daisy_D99】
难度:simple
本题同时也是《剑指offer 专项突击》的第6题【数组专题】.
problem
https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/
or
https://leetcode.cn/problems/kLl5u1/
给定一个已按照 升序排列 的整数数组 numbers ,请你从数组中找出两个数满足相加之和等于目标数 target .
函数应该以长度为 2 的整数数组的形式返回这两个数的下标值.numbers 的下标 从 0 开始计数 ,所以答案数组应当满足 0 <= answer[0] < answer[1] < numbers.length .
假设数组中存在且只存在一对符合条件的数字,同时一个数字不能使用两次.
类似的题
leetcode 1 号算法题:两数之和
leetcode 167 号算法题:两数之和Ⅱ - 输入有序数组
leetcode 170 号算法题:两数之和Ⅲ - 数据结构设计
leetcode 653 号算法题:两数之和Ⅳ - 输入 BST
leetcode 15 号算法题:三数之和
leetcode 18 号算法题:四数之和
哈希表
存 值-ID 对, save and find.
双指针
Double pointers for sorted arrays start at both ends.Move in the direction of the small,Move in the direction of the larger.
完整代码
class Solution:
def twoSum(self, numbers, target: int):
# hash 表: 值-ID
# dic ={}
# for i in range(len(numbers)):
# if target - numbers[i] in dic:
# return [dic[target-numbers[i]], i]
# dic[numbers[i]] = i
# 双指针
i = 0
j = len(numbers)-1
while i <= j:
if numbers[i]+numbers[j] == target:
return [i+1,j+1]
elif numbers[i]+ numbers[j]<target:
i +=1
else:
j-=1

边栏推荐
猜你喜欢

Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)

Public Key Retrieval is not allowed error solution

Navisworks切换语言

DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计

DTSE Tech Talk丨Phase 2: 1 hour in-depth interpretation of SaaS application system design

论文阅读 (63):Get To The Point: Summarization with Pointer-Generator Networks

3D激光SLAM:LeGO-LOAM论文解读---激光雷达里程计与建图

olap——入门ClickHouse

Leetcode 119. Yang Hui's Triangle II

Horizontal Pod Autoscaler(HPA)
随机推荐
Security business revenue growth rate exceeds 70% 360 builds digital security leader
[MRCTF2020]Ezaudit
Leetcode 118. 杨辉三角
Large-scale integrated office management system source code (OA+HR+CRM) source code sharing for free
Discuz杂志/新闻报道模板(jeavi_line)UTF8-GBK模板
优酷视频元素内容召回系统:多级多模态引擎探索
【SOC】经典输出hello world
Leetcode 119. 杨辉三角 II
node.js中怎么连接redis?
Rounding out the most practical way of several DLL injection
OpenCV形状检测
对话框 QDialog ( 详解 )
Google Cloud Spanner的实践经验
The case of five little pigs (five little pigs compare the size of the body weight)
MySQL 8.0.29 解压版安装教程(亲测有效)
3D激光SLAM:LeGO-LOAM论文解读---激光雷达里程计与建图
登录模块调试-软件调试入门
Login Module Debugging - Getting Started with Software Debugging
微信小程序picker滚动选择器使用详解
data storage
