当前位置:网站首页>[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
边栏推荐
- JUC forkjoin and completable future
- [算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
- Special palindromes of daily practice of Blue Bridge Cup
- 服务未正常关闭导致端口被占用
- GNSS定位精度指标计算
- Teach you to release a DeNO module hand in hand
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
- [leetcode15] sum of three numbers
- PRIDE-PPPAR源码解析
猜你喜欢
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
FairyGUI增益BUFF数值改变的显示
FairyGUI摇杆
In 2020, the average salary of IT industry exceeded 170000, ranking first
Latex learning
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
idea中导包方法
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
[算法] 剑指offer2 golang 面试题10:和为k的子数组
【无标题】
随机推荐
FairyGUI条子家族(滚动条,滑动条,进度条)
Naive Bayesian theory derivation
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
Get the position of the nth occurrence of the string
Matlab读取GNSS 观测值o文件代码示例
isEmpty 和 isBlank 的用法区别
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
闇の連鎖(LCA+树上差分)
Unity场景跳转及退出
Theoretical derivation of support vector machine
GPS高程拟合抗差中误差的求取代码实现
Fairygui loop list
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
InnoDB dirty page refresh mechanism checkpoint in MySQL
燕山大学校园网自动登录问题解决方案
SSD technical features
MySQL performance tuning - dirty page refresh
基于rtklib源码进行片上移植的思路分享
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
There is no red exclamation mark after SVN update