当前位置:网站首页>[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
题目1:
给定一个已按照 升序排列 的整数数组 numbers ,请你从数组中找出两个数满足相加之和等于目标数 target 。
函数应该以长度为 2 的整数数组的形式返回这两个数的下标值。numbers 的下标 从 0 开始计数 ,所以答案数组应当满足 0 <= answer[0] < answer[1] < numbers.length 。
假设数组中存在且只存在一对符合条件的数字,同时一个数字不能使用两次。
示例 1:
输入:numbers = [1,2,4,6,10], target = 8
输出:[1,3]
解释:2 与 6 之和等于目标数 8 。因此 index1 = 1, index2 = 3 。
示例 2:
输入:numbers = [2,3,4], target = 6
输出:[0,2]
示例 3:
输入:numbers = [-1,0], target = -1
输出:[0,1]
提示:
2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers 按 递增顺序 排列
-1000 <= target <= 1000
仅存在一个有效答案
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/kLl5u1
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路1:
//思路1: 双指针
//left,right
//如果左边加右边小于target,left ++
//如果左边加右边大于target,right --
//相等返回 {left,right}
代码
func twoSum(numbers []int, target int) []int {
//思路1: 双指针
//left,right
//如果左边加右边小于target,left ++
//如果左边加右边大于target,right --
//相等返回 {left,right}
//参数处理
if len(numbers) < 2 {
return nil
}
//双指针
left,right := 0,len(numbers) - 1
for left < right{
tempTarget := numbers[left] + numbers[right]
if tempTarget == target {
return []int{
left,right}
}else if tempTarget < target {
left ++
}else if tempTarget > target {
right --
}
}
//没有找到返回空
return nil
}
测试

边栏推荐
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- RTKLIB: demo5 b34f.1 vs b33
- How to reduce the shutdown time of InnoDB database?
- GNSS定位精度指标计算
- (课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
- How to improve the deletion speed of sequential class containers?
- About using @controller in gateway
- Common DOS commands
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- SSD technical features
猜你喜欢

第一人称视角的角色移动

(core focus of software engineering review) Chapter V detailed design exercises

單片機藍牙無線燒錄

编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
![Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]](/img/b0/176bf6dea2201afc892d6750c5974b.png)
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]

Halcon knowledge: gray_ Tophat transform and bottom cap transform

(5) Introduction to R language bioinformatics -- ORF and sequence analysis

FairyGUI增益BUFF数值改变的显示

Office prompts that your license is not genuine pop-up box solution

Pytorch: tensor operation (I) contiguous
随机推荐
Vulnhub target: hacknos_ PLAYER V1.1
[offer78] merge multiple ordered linked lists
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
【GNSS】抗差估计(稳健估计)原理及程序实现
Compile GDAL source code with nmake (win10, vs2022)
What are the advantages of using SQL in Excel VBA
In 2020, the average salary of IT industry exceeded 170000, ranking first
SVN更新后不出现红色感叹号
SSD technical features
JUC forkjoin and completable future
【干货】提升RTK模糊度固定率的建议之周跳探测
單片機藍牙無線燒錄
[offer9]用两个栈实现队列
rtklib单点定位spp使用抗差估计遇到的问题及解决
C programming exercise
InnoDB dirty page refresh mechanism checkpoint in MySQL
[leetcode19]删除链表中倒数第n个结点
341. Flatten nested list iterator
数据库课程设计:高校教务管理系统(含代码)