当前位置:网站首页>[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
边栏推荐
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
- What are the functions and features of helm or terrain
- 堆排序【手写小根堆】
- [Offer18]删除链表的节点
- [offer9]用两个栈实现队列
- FairyGUI简单背包的制作
- Matlab读取GNSS 观测值o文件代码示例
- Fairygui gain buff value change display
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- [算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
猜你喜欢
随机推荐
FairyGUI循环列表
Compile GDAL source code with nmake (win10, vs2022)
[算法] 劍指offer2 golang 面試題2:二進制加法
Office提示您的许可证不是正版弹框解决
Fairygui gain buff value change display
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
Unity scene jump and exit
KF UD分解之伪代码实现进阶篇【2】
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Conditional probability
Liste des boucles de l'interface graphique de défaillance
[offer9] implement queues with two stacks
[offer78] merge multiple ordered linked lists
程序设计大作业:教务管理系统(C语言)
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
Meanings and differences of PV, UV, IP, VV, CV
[leetcode15] sum of three numbers
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
堆排序【手写小根堆】