当前位置:网站首页>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
边栏推荐
猜你喜欢
华为云数据治理生产线DataArts,让“数据‘慧’说话”
华为云数据治理生产线DataArts,让“数据‘慧’说话”
2022-07-30 Androd 进入深度休眠后把WIFI给关掉,唤醒之后重新打开WIFI
【Linux操作系统】 虚拟文件系统 | 文件缓存
大厂面试官眼中的好简历到底长啥样
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
arcpy tutorial
23. Please talk about the difference between IO synchronization, asynchronous, blocking and non-blocking
【SOC】Classic output hello world
Wanhua chemical fine chemical industry innovation product assembly
随机推荐
数据的存储
《痞子衡嵌入式半月刊》 第 59 期
服务器装好系统的电脑怎么分区
Deep Feedback Network for Recommendation
归一化与标准化
PHP留言反馈管理系统源码
Discuz杂志/新闻报道模板(jeavi_line)UTF8-GBK模板
【AAAI2020】阿里DMR:融合Matching思想的深度排序模型
【Linux操作系统】 虚拟文件系统 | 文件缓存
阿里巴巴CAN:Embedding前置的特征交互新思路
lotus 爆块失败
MySQL详细学习教程(建议收藏)
探究CSAPP实验二-bomb lab-第一节
MySql统计函数COUNT详解
Mongoose模块
华为云数据治理生产线DataArts,让“数据‘慧’说话”
onenote使用
The way of life, share with you!
[Geek Challenge 2020] Roamphp1-Welcome
olap——入门ClickHouse