当前位置:网站首页>[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
边栏推荐
- Fairygui loop list
- Unity3d, Alibaba cloud server, platform configuration
- [Offer29] 排序的循环链表
- Naive Bayesian theory derivation
- First use of dosbox
- [算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
- In 2020, the average salary of IT industry exceeded 170000, ranking first
- Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- Compile GDAL source code with nmake (win10, vs2022)
猜你喜欢
[算法] 剑指offer2 golang 面试题10:和为k的子数组
[算法] 剑指offer2 golang 面试题1:整数除法
2021.11.10 compilation examination
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
Unity3D制作注册登录界面,并实现场景跳转
RTKLIB: demo5 b34f.1 vs b33
Fabrication of fairygui simple Backpack
FairyGUI循环列表
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
随机推荐
[leetcode622] design circular queue
idea中导包方法
Fairygui joystick
KF UD分解之UD分解基础篇【1】
Fabrication of fairygui simple Backpack
Fairygui gain buff value change display
Fabrication d'un sac à dos simple fairygui
Conditional probability
GPS高程拟合抗差中误差的求取代码实现
Halcon knowledge: gray_ Tophat transform and bottom cap transform
2021.11.10汇编考试
FairyGUI条子家族(滚动条,滑动条,进度条)
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
FairyGUI复选框与进度条的组合使用
[offer18] delete the node of the linked list
Compile GDAL source code with nmake (win10, vs2022)
堆排序【手写小根堆】
rtklib单点定位spp使用抗差估计遇到的问题及解决
Fairygui loop list
2022国赛Re1 baby_tree