当前位置:网站首页>[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条子家族(滚动条,滑动条,进度条)
Derivation of logistic regression theory
FairyGUI简单背包的制作
[算法] 剑指offer2 golang 面试题2:二进制加法
[Chongqing Guangdong education] Shandong University College Physics reference materials
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
地球围绕太阳转
In 2020, the average salary of IT industry exceeded 170000, ranking first
FairyGUI增益BUFF数值改变的显示
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
随机推荐
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
FairyGUI条子家族(滚动条,滑动条,进度条)
[offer18] delete the node of the linked list
[offer78] merge multiple ordered linked lists
[leetcode622] design circular queue
Containers and Devops: container based Devops delivery pipeline
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
[Offer18]删除链表的节点
First use of dosbox
isEmpty 和 isBlank 的用法区别
[Chongqing Guangdong education] Shandong University College Physics reference materials
Unity场景跳转及退出
使用rtknavi进行RT-PPP测试
雇佣收银员【差分约束】
【无标题】
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
程序设计大作业:教务管理系统(C语言)
MySQL error warning: a long semaphore wait