当前位置:网站首页>[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
边栏推荐
- FairyGUI简单背包的制作
- rtklib单点定位spp使用抗差估计遇到的问题及解决
- Derivation of logistic regression theory
- [Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
- [Leetcode15]三数之和
- KF UD分解之UD分解基础篇【1】
- Unity3D,阿里云服务器,平台配置
- Special palindromes of daily practice of Blue Bridge Cup
- [算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
- Mixed use of fairygui button dynamics
猜你喜欢

第一人称视角的角色移动

Teach you to release a DeNO module hand in hand

Design and implementation of general interface open platform - (39) simple and crude implementation of API services

Mysql database index

(core focus of software engineering review) Chapter V detailed design exercises

Vulnhub target: hacknos_ PLAYER V1.1

Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)

Fairygui loop list

Halcon knowledge: gray_ Tophat transform and bottom cap transform
![[算法] 剑指offer2 golang 面试题10:和为k的子数组](/img/63/7422489d09a64ec9f0e79378761bf1.png)
[算法] 剑指offer2 golang 面试题10:和为k的子数组
随机推荐
[Leetcode15]三数之和
Get the position of the nth occurrence of the string
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
Fabrication of fairygui simple Backpack
Liste des boucles de l'interface graphique de défaillance
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
音乐播放(Toggle && PlayerPrefs)
GNSS定位精度指标计算
[leetcode19] delete the penultimate node in the linked list
Theoretical derivation of support vector machine
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
FairyGUI简单背包的制作
Containers and Devops: container based Devops delivery pipeline
Fairygui loop list
FairyGUI条子家族(滚动条,滑动条,进度条)
服务未正常关闭导致端口被占用
Naive Bayesian theory derivation
Fabrication d'un sac à dos simple fairygui
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
Derivation of logistic regression theory