当前位置:网站首页>[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

边栏推荐
- It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
- Mixed use of fairygui button dynamics
- [Offer29] 排序的循环链表
- Fairygui loop list
- [Chongqing Guangdong education] Shandong University College Physics reference materials
- [Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- How to reduce the shutdown time of InnoDB database?
- Matlab读取GNSS 观测值o文件代码示例
- [leetcode15] sum of three numbers
猜你喜欢

FairyGUI复选框与进度条的组合使用

C programming exercise
![[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire

编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)

Guided package method in idea

Unity3D,阿里云服务器,平台配置

Vulnhub target: hacknos_ PLAYER V1.1
![[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组

FairyGUI摇杆
![[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
随机推荐
What are the advantages of using SQL in Excel VBA
HCIP Day 12
FairyGUI循環列錶
Introduction to the daily practice column of the Blue Bridge Cup
MySQL shutdown is slow
(1) Introduction Guide to R language - the first step of data analysis
程序设计大作业:教务管理系统(C语言)
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
Game 280 weekly
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
[offer18] delete the node of the linked list
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Latex learning
Single chip Bluetooth wireless burning
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
What are the functions and features of helm or terrain
[算法] 剑指offer2 golang 面试题10:和为k的子数组
[算法] 剑指offer2 golang 面试题1:整数除法
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组