当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 7: Array and 0 Of 3 A digital
subject 1:
Given an inclusion n Array of integers nums, Judge nums Are there three elements in a ,b ,c , bring a + b + c = 0 ? Please find all and for 0 And No repetition Triple of .
Example 1:
Input :nums = [-1,0,1,2,-1,-4]
Output :[[-1,-1,2],[-1,0,1]]
Example 2:
Input :nums = []
Output :[]
Example 3:
Input :nums = [0]
Output :[]
Tips :
0 <= nums.length <= 3000
-105 <= nums[i] <= 105
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/1fGaJU
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas 1:
// Ideas : Double pointer
// First, sort the array
// Fix a number nums[i], other 2 The sum of the numbers is -nums[i] that will do , It's a double pointer problem
// duplicate removal ,i,j If you encounter duplicate numbers, you can skip them and go to the duplicate
Code
func threeSum(nums []int) [][]int {
// Ideas : Double pointer
// First, sort the array
// Fix a number nums[i], other 2 The sum of the numbers is -nums[i] that will do , It's a double pointer problem
// duplicate removal ,i,j If you encounter duplicate numbers, you can skip them and go to the duplicate
res := make([][]int,0)
// Processing parameters
if len(nums) < 3 {
return res
}
// Sort
sort.Slice(nums,func(i,j int) bool{
return nums[i] < nums[j]
})
// Fix a number nums[i]
for i := 0; i < len(nums) -2 ; {
// Double pointer
j,k := i+1,len(nums) -1
for j < k {
temp := nums[i] + nums[j] + nums[k]
if temp == 0 {
res = append(res,[]int{
nums[i],nums[j],nums[k]})
// duplicate removal
jVal := nums[j]
for j < k && jVal == nums[j]{
j ++
}
}else if temp < 0 {
j ++
}else {
k --
}
}
// duplicate removal
iVal := nums[i]
for i < len(nums) && iVal == nums[i]{
i ++
}
}
return res
}
test
边栏推荐
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- [offer29] sorted circular linked list
- Get the position of the nth occurrence of the string
- Teach you to release a DeNO module hand in hand
- 地球围绕太阳转
- idea问题记录
- 微信小程序开发心得
- FairyGUI人物状态弹窗
- 【无标题】
- Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
猜你喜欢
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
RTKLIB: demo5 b34f.1 vs b33
【干货】提升RTK模糊度固定率的建议之周跳探测
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
Theoretical derivation of support vector machine
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
编辑距离(多源BFS)
2021.11.10汇编考试
Guided package method in idea
2022国赛Re1 baby_tree
随机推荐
PRIDE-PPPAR源码解析
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
燕山大学校园网自动登录问题解决方案
Naive Bayesian theory derivation
Vulnhub target: hacknos_ PLAYER V1.1
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
Office prompts that your license is not genuine pop-up box solution
[算法] 剑指offer2 golang 面试题1:整数除法
[offer29] sorted circular linked list
NRF24L01 troubleshooting
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
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
dosbox第一次使用
C programming exercise
MySQL error warning: a long semaphore wait
Guided package method in idea
Unity scene jump and exit