问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3618 访问。

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]


Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:The solution set must not contain duplicate triplets.

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]

示例

public class Program {

    public static void Main(string[] args) {
var intervals = new int[] { -1, 0, 1, 2, -1, -4 }; var res = ThreeSum(intervals);
ShowArray(res); intervals = new int[] { 0, 0, 0 }; res = ThreeSum2(intervals);
ShowArray(res); Console.ReadKey();
} private static void ShowArray(IList<IList<int>> list) {
foreach(var nums in list) {
foreach(var num in nums) {
Console.Write($"{num} ");
}
Console.WriteLine();
}
} public static IList<IList<int>> ThreeSum(int[] nums) {
//暴力解法肯定超时无法AC
var res = new List<IList<int>>();
for(int i = 0; i < nums.Length; i++) {
for(int j = 0; j < nums.Length; j++) {
for(int k = 0; k < nums.Length; k++) {
if(nums[i] + nums[j] + nums[k] == 0 &&
i != j && j != k && k != i) {
var line = new List<int> { nums[i], nums[j], nums[k] };
line = line.OrderBy(r => r).ToList();
if(!res.Any(l => l[0] == line[0] &&
l[1] == line[1] &&
l[2] == line[2])) {
res.Add(line);
}
}
}
}
}
return res;
} public static IList<IList<int>> ThreeSum2(int[] nums) {
//先固定 i,再计算三数之和是不是0
//需认真分析是否有重复的三元组
var res = new List<IList<int>>();
var length = nums.Length;
Array.Sort(nums);
for(var i = 0; i < length - 2; i++) {
//定义左右指针j和k
//i为循环变量,可以认为先固定了i
var j = i + 1;
var k = length - 1;
//分析j和k
while(j < k) {
//若和大于0,则需要减整体的值,将右指针 k 减1
if(nums[i] + nums[j] + nums[k] > 0) k--;
//若和小于0,则需要加整体的值,将左指针 j 加1
else if(nums[i] + nums[j] + nums[k] < 0) j++;
else {
//若和正好等于0,将其存放进结果中
//然后需要处理重复三元组的情况
//不重复三元组需要3个索引各不相同并且组成的值的结果不能重复
//以下3个条件组成了满足不重复三元组的所有条件
//条件1:i、j、k的顺序具有稳定性,总是从小到大
res.Add(new int[] { nums[i], nums[j], nums[k] });
//首先使用 j + 1 <= nums.Length - 1 保证数组不越界
//条件2:再向右找到第1个值不和当前左指针相同的值的索引
//若为 1,1,1,1,1,2,找到2
while(j + 1 <= nums.Length - 1 && nums[j + 1] == nums[j++]) { }
}
}
//条件3:向右找到最后1个和当前被固定的 i 的值相同的值的索引
//若为 1,1,1,1,1,2,找到2前面的最后一个1
while(i + 1 <= nums.Length - 1 && nums[i + 1] == nums[i]) i++;
}
return res;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3618 访问。

-1 0 1
-1 -1 2
0 0 0

分析

显而易见, ThreeSum 的时间复杂度为: O(n3)O(n^3)O(n3),ThreeSum2 的时间复杂度为:O(n2)O(n^2)O(n2) 。

C#LeetCode刷题之#15-三数之和(3Sum)的更多相关文章

  1. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  2. Leetcode题库——15.三数之和

    @author: ZZQ @software: PyCharm @file: threeSum.py @time: 2018/10/6 19:47 说明:给定一个包含 n 个整数的数组 nums,判断 ...

  3. leetcode刷题笔记-1. 两数之和(java实现)

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...

  4. LeetCode 刷题笔记 1. 两数之和(Two Sum)

    tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...

  5. #leetcode刷题之路15-三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  6. #leetcode刷题之路1-两数之和

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...

  7. (1)leetcode刷题Python笔记——两数之和

    题目如下: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...

  8. #leetcode刷题之路18-四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  9. C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...

  10. Java实现 LeetCode 15 三数之和

    15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...

随机推荐

  1. redis的集群化方案

    关于 目前有三种 (1)Twitter开发的twemproxy (2)豌豆荚开发的codis (3)redis官方的redis-cluster Twemproxy 架构简单 就是用proxy对后端re ...

  2. JavaWeb基础(day15)( http + tomcat + servlet + 响应)

    HTTP+Tomcat+Servlet+响应 HTTP HTTP  超文本传输协议(Hyper Text  Transfer  Protocol  ),一种网络协议. 协议的组成和过程 HTTP协议由 ...

  3. 【C#】NET截屏网页,生成网页快照开发——IECapt、CutyCapt

    软件介绍 IECapt.CutyCapt 生成网页快照 http://iecapt.sourceforge.net/ http://cutycapt.sourceforge.net/ ### 操作代码 ...

  4. .Net Core+Nginx实现项目负载均衡

    nginx大家如果没用过那或多或少都应该听过,vue的部署.反向代理.负载均衡nginx都能帮你做到. 今天主要说一下nginx负载均衡我们的项目,如下图所示,请求到达nginx,nginx再帮我们转 ...

  5. git配置httpd服务-web_dav模式

    1,搭建httpd应用 2,修改httpd.conf文件 注释 DocumentRoot "/data/httpd/htdocs" 注释 <Directory "/ ...

  6. FaaS 给前端带来了什么?

    一.Serverless 与 FaaS Serverless 是一种云计算理念,即无服务器计算(Serverless Computing): Serverless suggests that the ...

  7. Zookeeper ----- 系统模型

    数据模型 Zookeeper的数据模型与文件系统非常相似,唯一不同的它的每个节点(ZNode)都可以存放数据,无论父节点还是子节点. 事务ID 即前面提到的ZXID.对每个事务请求,Zookeeper ...

  8. layer弹窗插件留言提交

    function msgShow(getname,getuserid){ layer.open({ type: 1 //此处以iframe举例 ,title: '收件人:'+getname+'(ID: ...

  9. PHP date_default_timezone_set() 函数

    ------------恢复内容开始------------ 实例 设置默认时区: <?php date_default_timezone_set("Asia/Shanghai&quo ...

  10. PHP pclose() 函数

    定义和用法 pclose() 函数关闭由 popen() 打开的进程. 如果失败,该函数返回 FALSE. 语法 pclose(pipe) 参数 描述 pipe 必需.规定由 popen() 打开的进 ...