当前位置:网站首页>[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
边栏推荐
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- FairyGUI条子家族(滚动条,滑动条,进度条)
- Fairygui character status Popup
- 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
- 基本Dos命令
- [算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
- Prove the time complexity of heap sorting
- [offer9] implement queues with two stacks
- [算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
- [算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
猜你喜欢
随机推荐
How to improve the deletion speed of sequential class containers?
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
[algorithm] sword finger offer2 golang interview question 1: integer division
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Force buckle 1189 Maximum number of "balloons"
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
Compile GDAL source code with nmake (win10, vs2022)
Programming homework: educational administration management system (C language)
What is the maximum length of MySQL varchar field
最短Hamilton路径 (状压DP)
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
dosbox第一次使用
MySQL error warning: a long semaphore wait
基本Dos命令
编辑距离(多源BFS)
Office提示您的许可证不是正版弹框解决
FairyGUI简单背包的制作
Easy to use shortcut keys in idea
Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
Database table splitting strategy








![[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组](/img/8c/1b6ba3b1830ad28176190170c98628.png)
