数组中找三个数和为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. NSoup解析处理Html

    以前在做网页静态生成的时候,使用正则表达式分析提取网页链接.最近搜索了解到java有个Jsoup解析网页,对应.net有个nsoup.处理网页非常好用. Document doc = NSoupCli ...

  2. 后台发送http请求通用方法,包括get和post

    package com.examsafety.service.sh; import java.io.BufferedReader; import java.io.IOException; import ...

  3. Codeforces Round #319 (Div. 2)B. Modulo Sum DP

                                                             B. Modulo Sum                               ...

  4. createTextNode和innerHTML什么区别

    今天写代码,用到createTextNode,发现好像功能和innerHTML是一样的,于是查阅了网上的资料了. 一.createTextNode 例如: var element = document ...

  5. Sqoop异常解决ERROR tool.ImportTool: Encountered IOException running import job: java.io.IOException: No columns to generate for ClassWriter问题

    问题详情如下: 解决办法 这个是由于mysql-connector-java的bug造成的,出错时我用的是mysql-connector-java-5.1.10-bin.jar,更新成mysql-co ...

  6. YTU 2547: Repairing a Road

    2547: Repairing a Road 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 2 题目描述 You live in a small town with R b ...

  7. linux 和win7 双系统模式下 忘记win7 密码的修改方法

    首先登陆linux系统,在linux 系统下找到win7系统的安装盘(比如为C盘), (1)进入到C://windows/system32下 找到osk.exe 文件,并将其剪切到其他的地方(记住这个 ...

  8. 就是这个foxmail有时出现记事同步不了

    昨天和妈妈商量还是要打算再次买一块正版电池,虽然她现在有点不情愿,但是过一个月再说,然后就是他的号码超出流量6M,由于是2G的流量,按照每M是一元,所以就是扣了6元,然后他的套餐里面显示还有94M也不 ...

  9. debian下使用dpkg来安装/卸载deb包 (转载)

    转自:http://blog.csdn.net/zhou_2008/article/details/6076900 在debian下,你可以使用dpkg(Debian package system)来 ...

  10. js中return的作用及用法

    这里面的return含有一些细节知识: 例如:onClick='return add_onclick()'与 onClick='add_onclick()'的区别 JAVASCRIPT在事件中调用函数 ...