题目英文

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.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],A solution set is:
[[-1, 0, 1],[-1, -1, 2]
]

题目中文

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

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

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:
[[-1, 0, 1],[-1, -1, 2]
]

算法实现

public class Solution {public IList<IList<int>> ThreeSum(int[] nums) {IList<IList<int>> result = new List<IList<int>>();if (nums == null || nums.Length < 3)return result;nums = nums.OrderBy(a => a).ToArray();int len = nums.Length;for (int i = 0; i < len; i++){if (nums[i] > 0) // 如果当前数字大于0,则三数之和一定大于0,所以结束循环break;if (i > 0 && nums[i] == nums[i - 1])continue; // 去重int l = i + 1;int r = len - 1;while (l < r){int sum = nums[i] + nums[l] + nums[r];if (sum == 0){result.Add(new List<int>() {nums[i], nums[l], nums[r]});while (l < r && nums[l] == nums[l + 1]) l++; // 去重while (l < r && nums[r - 1] == nums[r]) r--; //去重l++;r--;}else if (sum < 0){l++;}else if (sum > 0){r--;}}}return result;    }
}

实验结果

  • 状态:通过
  • 313 / 313 个通过测试用例
  • 执行用时:468 ms


相关图文

  • LeetCode实战:删除链表的倒数第N个节点
  • LeetCode实战:合并两个有序链表
  • LeetCode实战:两两交换链表中的节点
  • LeetCode实战:旋转链表
  • LeetCode实战:相同的树
  • LeetCode实战:对称二叉树
  • LeetCode实战:二叉树的最大深度
  • LeetCode实战:搜索二维矩阵
  • LeetCode实战:将有序数组转换为二叉搜索树
  • 资料分享:数学建模资料分享 – 图论部分
  • 资料分享:数学建模资料分享 – 神经网络部分
  • 如何利用 C# 实现 K 最邻近算法?
  • 如何利用 C# 实现 K-D Tree 结构?
  • 如何利用 C# + KDTree 实现 K 最邻近算法?
  • 如何利用 C# 对神经网络模型进行抽象?
  • 如何利用 C# 实现神经网络的感知器模型?
  • 如何利用 C# 实现 Delta 学习规则?
  • 如何利用 C# 实现 误差反向传播 学习规则?
  • 如何利用 C# 爬取带 Token 验证的网站数据?
  • 如何利用 C# 向 Access 数据库插入大量数据?
  • 如何利用 C# + Python 破解猫眼电影的反爬虫机制?

LeetCode实战:三数之和相关推荐

  1. [双指针|模拟] leetcode 15 三数之和

    [双指针|模拟] leetcode 15 三数之和 1.题目 题目链接 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ? ...

  2. LeetCode 15三数之和16最接近的三数之和

    三数之和(双指针) 题意: 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意 ...

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

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

  4. 20200126:(leetcode)三数之和 最接近的三数之和(含图解)

    三数之和 && 最接近的三数之和 题目 基本思路 代码实现 题目 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b ...

  5. Leetcode 15.三数之和

    Time: 20190920 Type: Medium 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所 ...

  6. Leetcode 15:三数之和(最详细解决方案!!!)

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

  7. Java实现 LeetCode 15 三数之和

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

  8. 用 PHP 来刷leetCode 之 三数之和

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

  9. LeetCode 15. 三数之和【双指针】

    15. 三数之和 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j.i != k 且 j != k ,同时还满足 nums[ ...

  10. LeetCode 15. 三数之和

    题目描述 15. 三数之和 思路 思路1 比较容易想到的就是,求三数之和等于0,可以等价于求两个数的和,然后看这个和的相反数是否在nums里面. 但是 T_T这样的话复杂度太高了,会超时,捂脸,最后三 ...

最新文章

  1. jquery each
  2. call,apply,bind,new实现原理
  3. 在IIS中写Python的CGI脚本
  4. 织梦配置多个mysql_一台机器,多个mysqld服务
  5. AcWing 831. KMP字符串(模板)
  6. 计算机视觉模型、学习和推理
  7. 7.9模拟赛T1图的遍历(dfs)
  8. 基于MSFM算法与最速下降法的射线追踪技术
  9. Log4j配置学习文档之二 处理日滚文件-实现原理
  10. (转)Linux传输大文件(分割传输)
  11. 开启 ASA 5505 snmp协议
  12. quartz spring 时间配置
  13. led闪烁和流水灯代码
  14. 2021新版CISSP考试大纲解析
  15. 基于Python的淘宝用户行为数据分析
  16. et al和etc区别
  17. 3d打开无法下载star.php,下载的3dmax模型打开失败的原因及解决方法
  18. 2019产品数据管理(PDM)技术说明
  19. 简单介绍一下web开发中用到的一些技术
  20. 快速解决Solving environment: failed with initial frozen solve. Retrying with flexible solve

热门文章

  1. tensorflow with求导_3.4tensorflow2.x自动求导原理函数详解
  2. java培训教程分享:Java编写软件代码自动提示功能
  3. 【TeeChart Pro ActiveX教程】(八):ADO数据库访问(上)
  4. C语言比较好的风格梳理
  5. Python加密—RSA加密
  6. (转)java 中的try catch finally 语句中含有return语句的执行情况(总结版)
  7. css的background
  8. Oracle Grid Control 10.2.0.5 for Linux 安装和配置指南
  9. Ubuntu中Atom安装与使用
  10. 解决Attempt to execute SCRIPT mexLasso as a function