当前位置:网站首页>[算法] 剑指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
}
测试

边栏推荐
- Excel导入,导出功能实现
- 程序设计大作业:教务管理系统(C语言)
- [Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
- [leetcode15] sum of three numbers
- Database table splitting strategy
- Fabrication of fairygui simple Backpack
- @The difference between Autowired and @resource
- Lock wait timeout exceeded try restarting transaction
- Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
猜你喜欢

Latex learning

dosbox第一次使用

Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance

数据库课程设计:高校教务管理系统(含代码)

Fairygui character status Popup

(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart

FairyGUI循环列表

Naive Bayesian theory derivation

Excel导入,导出功能实现

idea中好用的快捷键
随机推荐
idea问题记录
MySQL time, time zone, auto fill 0
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Itext 7 生成PDF总结
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
FairyGUI簡單背包的制作
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
Unity3D制作注册登录界面,并实现场景跳转
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Latex learning
地球围绕太阳转
Conditional probability
Unity场景跳转及退出
[Offer29] 排序的循环链表
Acwing-116 pilot brother
RTKLIB: demo5 b34f.1 vs b33