当前位置:网站首页>[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
题目1:
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a ,b ,c ,使得 a + b + c = 0 ?请找出所有和为 0 且 不重复 的三元组。
示例 1:
输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
示例 2:
输入:nums = []
输出:[]
示例 3:
输入:nums = [0]
输出:[]
提示:
0 <= nums.length <= 3000
-105 <= nums[i] <= 105
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/1fGaJU
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路1:
//思路: 双指针
//首先将数组排序
//固定一个数字nums[i],其他2个数字的和为-nums[i]即可,就是一个双指针问题
//去重,i,j遇到重复的数字就跳过就能去重
代码
func threeSum(nums []int) [][]int {
//思路: 双指针
//首先将数组排序
//固定一个数字nums[i],其他2个数字的和为-nums[i]即可,就是一个双指针问题
//去重,i,j遇到重复的数字就跳过就能去重
res := make([][]int,0)
//参数处理
if len(nums) < 3 {
return res
}
//排序
sort.Slice(nums,func(i,j int) bool{
return nums[i] < nums[j]
})
//固定一个数字 nums[i]
for i := 0; i < len(nums) -2 ; {
//双指针
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]})
//去重
jVal := nums[j]
for j < k && jVal == nums[j]{
j ++
}
}else if temp < 0 {
j ++
}else {
k --
}
}
//去重
iVal := nums[i]
for i < len(nums) && iVal == nums[i]{
i ++
}
}
return res
}
测试
边栏推荐
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
- FairyGUI摇杆
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- 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
- Agile development helps me
- NRF24L01故障排查
- How to add music playback function to Arduino project
- [offer9] implement queues with two stacks
- Derivation of logistic regression theory
- SVN更新后不出现红色感叹号
猜你喜欢

使用rtknavi进行RT-PPP测试

Matlab读取GNSS 观测值o文件代码示例
![[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等](/img/11/ee0628a68542236fc641966579a31a.png)
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等

FairyGUI增益BUFF数值改变的显示

Mysql database index

Unity3D,阿里云服务器,平台配置

FairyGUI簡單背包的制作

The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan

In 2020, the average salary of IT industry exceeded 170000, ranking first

FairyGUI循环列表
随机推荐
Guided package method in idea
Servlet
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
Detailed explanation of truncate usage
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
Unity3D,阿里云服务器,平台配置
wsl常用命令
How to improve the deletion speed of sequential class containers?
Fairygui loop list
[leetcode19] delete the penultimate node in the linked list
Expected value (EV)
Idea problem record
NovAtel 板卡OEM617D配置步骤记录
PRIDE-PPPAR源码解析
[offer78]合并多个有序链表
【无标题】
Unity scene jump and exit
Flink late data processing (3)
Minio file download problem - inputstream:closed
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface