数组中找三个数和为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. 2016/1/17 笔记 1,面向对象编程OOP 2,类 全

    面向对象编程OOP 编程方式的发展 1,面向过程 重用性低 维护工作量大  2,面向对象 重用性高 维护容易 概念 1,对象 Object 一个具体的事物 是类的实例      2,类Class 同一 ...

  2. luogu 3952 时间复杂度

    noip2017 D1T2 时间复杂度 某zz选手考场上写了1.5h 考完之后发现自己写的是错的 但是结果A了??? 题目大意: 一种新的编程语言 A++ 给出一个程序只有循环语句 并给出这个程序的时 ...

  3. shell脚本执行错误:#!/bin/bash: No such file or directory

    执行.sh脚本时控制台报错 : #!/bin/bash: No such file or directory 解决办法: cat -A 文件路径 会发现第一行有问题 M-oM-;M-?#!/bin/b ...

  4. Java序列化系列教程(上)

    一定义以及相关概念 互联网的产生带来了机器间通讯的需求,而互联通讯的双方需要采用约定的协议,序列化和反序列化属于通讯协议的一部分.通讯协议往往采用分层模型,不同模型每层的功能定义以及颗粒度不同,例如: ...

  5. 关于file文件操作的头文件 【LINUX】 (转载)

    转自:http://blog.csdn.net/figo77ll/article/details/3156052 Linux下如果要对文件进行读取访问,需要包含至少以下两个头文件: #inlcude ...

  6. bzoj 1725: [Usaco2006 Nov]Corn Fields牧场的安排【状压dp】

    压一维状态,转移时把符合条件的上一行加上 #include<iostream> #include<cstdio> using namespace std; const int ...

  7. bzoj 1642: [Usaco2007 Nov]Milking Time 挤奶时间【dp】

    这不就是个n方dp吗--看了眼洛谷题解简直神仙打架 我全程没用到n-- 把休息时间并入产奶时间,注意"结束时间不挤奶",所以ei=ei+r-1,注意这个-1! 然后按r排序,设f[ ...

  8. lodop 打印

    使用Lodop打印: 一.在官网下载http://www.lodop.net/download.html 若是安装还是提示未安装,就按转这个 二.准备两个js 三.需要在页面最上面加入 <htm ...

  9. vs2017 + miniUI 后端框架使用

    vs2017 +  miniUI  后端框架使用 上miniUI官网直接下载框架.http://www.miniui.com/ 此框架使用说明很清楚. 2.1.vs2017创建安装miniUI后端框架 ...

  10. android_app c++框架

    找遍了全网,没有一个完整的可用的框架.ndk自带的android_native_app_glue确实不太好用,闭关几天,写出了一个框架.完全的消息队列调用,目前测试的主体框架是没有什么问题了,程序入口 ...