当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 6: Sort the sum of two numbers in the array
subject 1:
Given a has been according to Ascending order Array of integers for numbers , Please find out two numbers from the array, and the sum of them is equal to the target number target .
Functions should be length based 2 Returns the subscript values of the two numbers in the form of an array of integers .numbers The subscript from 0 Start counting , So the answer array should satisfy 0 <= answer[0] < answer[1] < numbers.length .
Suppose that there is only one pair of qualified numbers in the array , At the same time, a number cannot be used twice .
Example 1:
Input :numbers = [1,2,4,6,10], target = 8
Output :[1,3]
explain :2 And 6 The sum is equal to the number of targets 8 . therefore index1 = 1, index2 = 3 .
Example 2:
Input :numbers = [2,3,4], target = 6
Output :[0,2]
Example 3:
Input :numbers = [-1,0], target = -1
Output :[0,1]
Tips :
2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers Press Increasing order array
-1000 <= target <= 1000
There is only one valid answer
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/kLl5u1
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas 1:
// Ideas 1: Double pointer
//left,right
// If left plus right is less than target,left ++
// If left plus right is greater than target,right --
// Equal return {left,right}
Code
func twoSum(numbers []int, target int) []int {
// Ideas 1: Double pointer
//left,right
// If left plus right is less than target,left ++
// If left plus right is greater than target,right --
// Equal return {left,right}
// Processing parameters
if len(numbers) < 2 {
return nil
}
// Double pointer
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 empty if not found
return nil
}
test
边栏推荐
- 第一人称视角的角色移动
- Excel导入,导出功能实现
- 基于rtklib源码进行片上移植的思路分享
- Office提示您的许可证不是正版弹框解决
- [Chongqing Guangdong education] Shandong University College Physics reference materials
- FairyGUI循环列表
- Unity3d makes the registration login interface and realizes the scene jump
- Derivation of logistic regression theory
- JUC forkjoin and completable future
- FairyGUI增益BUFF数值改变的显示
猜你喜欢
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
音乐播放(Toggle && PlayerPrefs)
Force buckle 1189 Maximum number of "balloons"
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
(1) Introduction Guide to R language - the first step of data analysis
FairyGUI循環列錶
Theoretical derivation of support vector machine
rtklib单点定位spp使用抗差估计遇到的问题及解决
随机推荐
Esp8266 connect onenet (old mqtt mode)
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Lock wait timeout exceeded try restarting transaction
Fabrication d'un sac à dos simple fairygui
程序设计大作业:教务管理系统(C语言)
【无标题】
First use of dosbox
GNSS定位精度指标计算
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
【GNSS】抗差估计(稳健估计)原理及程序实现
RTKLIB: demo5 b34f.1 vs b33
[算法] 劍指offer2 golang 面試題2:二進制加法
FairyGUI摇杆
NRF24L01 troubleshooting
Liste des boucles de l'interface graphique de défaillance
堆排序【手写小根堆】
Unity3D制作注册登录界面,并实现场景跳转
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce