数组中找三个数和为0的结果集
1 // 解法一:先排序 然后固定一个值 然后用求两个数的和的方式
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums.length < 3) {
return res;
}
Arrays.sort(nums);
// 遍历到倒数第三个就可以了
for (int i = 0; i < nums.length - 2; i++) {
// i去重复
if (i != 0 && nums[i] == nums[i - 1])
continue;
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
if (nums[i] + nums[j] + nums[k] == 0) {
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[k]);
res.add(list);
j++;
k--;
// 去重复
while (j < k && nums[j - 1] == nums[j]) {
j++;
}
while (j < k && nums[k] == nums[k + 1]) {
k--;
}
} else if (nums[i] + nums[j] + nums[k] < 0) {
j++;
} else {
k--;
}
}
}
return res;
}

LeetCode15——3Sum的更多相关文章

  1. LeetCode15 3Sum

    题意: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  2. leetcode15—3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  3. Leetcode15.3Sum三数之和

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

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

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

  5. ARTS第六周

    第六周.后期补完,太忙了. 1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有 ...

  6. [Swift]LeetCode15. 三数之和 | 3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  7. LeetCode: 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  8. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

  9. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

随机推荐

  1. Oculus Rift DK2 驱动安装教程

    第一次安装oculus rift硬件驱动的教程: 1.   执行驱动的下载网址:https://developer.oculusvr.com/ 下载驱动首先须要拥有一个oculus的帐号.点击Regi ...

  2. <form> 标签的entype属性

    entype属性规定在发送到服务器之前应该如何对表单数据进行编码. 属性值 描述 application/x-www-form-urlencoded 在发送前编码所有字符(默认) multipart/ ...

  3. C# winfrom TCP 服务端和客户端(链接)

    1.C#Winform TCP 之服务端: 可以参考下面链接,比较好.第二个链接可以看看,提供了一个思路. http://www.cnblogs.com/guolebin7/archive/2013/ ...

  4. python re正则表达式模块

    模块的的作用主要是用于字符串和文本处理,查找,搜索,替换等 复习一下基本的正则表达式吧  .:匹配除了换行符以为的任意单个字符  *:匹配任意字符,一个,零个,多个都能匹配得到 俗称贪婪模式 +:匹配 ...

  5. bzoj 1492: [NOI2007]货币兑换Cash【贪心+斜率优化dp+cdq】

    参考:http://www.cnblogs.com/lidaxin/p/5240220.html 虽然splay会方便很多,但是懒得写,于是写了cdq 首先要想到贪心的思路,因为如果在某天买入是能得到 ...

  6. bzoj 1664: [Usaco2006 Open]County Fair Events 参加节日庆祝【dp+树状数组】

    把长度转成右端点,按右端点排升序,f[i]=max(f[j]&&r[j]<l[i]),因为r是有序的,所以可以直接二分出能转移的区间(1,w),然后用树状数组维护区间f的max, ...

  7. bzoj1202: [HNOI2005]狡猾的商人(并查集 差分约束)

    1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4127  Solved: 1981[Submit][Sta ...

  8. HTML文档中class的命名规则以及命名规范

    1.采用英文字母.数字以及“-”和“_”命名. 2.以小写字母开头,不能以数字和“-”.“_”开头. 3.命名形式:单字,连字符,下划线和驼峰. 4.使用有意义命名. 其中(3).(4)条规定主要是便 ...

  9. php 编译时 报错 configure: error: libXpm.(a|so) not found.

    编译环境 centos7 php 5.4.26 $ yum install libXpm-devel 显示已安装 百度得知 ubuntu虚拟机安装lamp遇到的问题 configure: error: ...

  10. 推荐一波 瀑布流的RecylceView

    推荐博客:http://www.bubuko.com/infodetail-999014.html