当前位置:网站首页>[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
边栏推荐
- Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
- Unity3d, Alibaba cloud server, platform configuration
- FairyGUI简单背包的制作
- isEmpty 和 isBlank 的用法区别
- Devops' future: six trends in 2022 and beyond
- [Offer29] 排序的循环链表
- [offer78] merge multiple ordered linked lists
- Database course design: college educational administration management system (including code)
- 【GNSS】抗差估计(稳健估计)原理及程序实现
- [算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
猜你喜欢
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
NRF24L01 troubleshooting
Derivation of logistic regression theory
堆排序【手写小根堆】
基本Dos命令
341. Flatten nested list iterator
Matlab读取GNSS 观测值o文件代码示例
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
KF UD分解之UD分解基础篇【1】
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
随机推荐
Idea problem record
PRIDE-PPPAR源码解析
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
Fairygui character status Popup
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
Vulnhub target: hacknos_ PLAYER V1.1
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
isEmpty 和 isBlank 的用法区别
服务未正常关闭导致端口被占用
微信小程序开发心得
Unity3d, Alibaba cloud server, platform configuration
Mysql database index
(core focus of software engineering review) Chapter V detailed design exercises
[algorithm] sword finger offer2 golang interview question 1: integer division
Knowledge system of digital IT practitioners | software development methods -- agile
[Chongqing Guangdong education] Shandong University College Physics reference materials
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
平衡二叉树详解 通俗易懂
Office prompts that your license is not genuine pop-up box solution
KF UD分解之伪代码实现进阶篇【2】