当前位置:网站首页>[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
边栏推荐
- [算法] 剑指offer2 golang 面试题10:和为k的子数组
- [leetcode622] design circular queue
- Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
- NovAtel 板卡OEM617D配置步骤记录
- 地球围绕太阳转
- Guided package method in idea
- There is no red exclamation mark after SVN update
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- (3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
- PRIDE-PPPAR源码解析
猜你喜欢

【无标题】

Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
![[algorithm] sword finger offer2 golang interview question 1: integer division](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[algorithm] sword finger offer2 golang interview question 1: integer division
![[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等](/img/11/ee0628a68542236fc641966579a31a.png)
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等

Mysql database index
![[algorithm] sword finger offer2 golang interview question 2: binary addition](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[algorithm] sword finger offer2 golang interview question 2: binary addition

KF UD分解之UD分解基础篇【1】

Prove the time complexity of heap sorting

On March 15, the official version of go 1.18 was released to learn about the latest features and usage

Liste des boucles de l'interface graphique de défaillance
随机推荐
InnoDB dirty page refresh mechanism checkpoint in MySQL
【干货】提升RTK模糊度固定率的建议之周跳探测
服务未正常关闭导致端口被占用
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
[leetcode622]设计循环队列
What are the advantages of using SQL in Excel VBA
Fairygui loop list
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Matlab读取GNSS 观测值o文件代码示例
Mixed use of fairygui button dynamics
燕山大学校园网自动登录问题解决方案
Fabrication of fairygui simple Backpack
【RTKLIB 2.4.3 b34 】版本更新简介一
KF UD分解之伪代码实现进阶篇【2】
[算法] 剑指offer2 golang 面试题1:整数除法
Unity场景跳转及退出
[offer9]用两个栈实现队列
Itext 7 生成PDF总结
Vulnhub target: hacknos_ PLAYER V1.1