当前位置:网站首页>[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
边栏推荐
- Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
- InnoDB dirty page refresh mechanism checkpoint in MySQL
- 平衡二叉树详解 通俗易懂
- 编辑距离(多源BFS)
- Special palindromes of daily practice of Blue Bridge Cup
- Halcon knowledge: gray_ Tophat transform and bottom cap transform
- [offer29] sorted circular linked list
- Fairygui character status Popup
- Conditional probability
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
猜你喜欢

2022国赛Re1 baby_tree

Fabrication of fairygui simple Backpack
![[算法] 剑指offer2 golang 面试题1:整数除法](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[算法] 剑指offer2 golang 面试题1:整数除法

微信小程序开发心得

使用rtknavi进行RT-PPP测试

What are the advantages of using SQL in Excel VBA

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

FairyGUI条子家族(滚动条,滑动条,进度条)

Guided package method in idea

Fairygui character status Popup
随机推荐
[Chongqing Guangdong education] Shandong University College Physics reference materials
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
[algorithm] sword finger offer2 golang interview question 2: binary addition
Excel导入,导出功能实现
How to reduce the shutdown time of InnoDB database?
地球围绕太阳转
KF UD分解之UD分解基础篇【1】
2022国赛Re1 baby_tree
[算法] 剑指offer2 golang 面试题1:整数除法
Unity3d makes the registration login interface and realizes the scene jump
[leetcode19]删除链表中倒数第n个结点
[算法] 剑指offer2 golang 面试题2:二进制加法
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
Prove the time complexity of heap sorting
FairyGUI增益BUFF数值改变的显示
Expected value (EV)
【RTKLIB 2.4.3 b34 】版本更新简介一