【LeetCode】15. 3Sum 三个数和为0
题目:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
思路:先对数组排序,for循环选定每个位置的数,在下个位置及末尾设置两个索引,从两边往中间走,找出两个数满足三者的和为0。因为数组有序,所以当和大于0时,右边索引左移,反之,左边索引右移。
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result=new ArrayList<List<Integer>>();
int len=nums.length;
if(len<3) return result;
int sum=0;
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
if(nums[i]>0) break;
if(i>0&&nums[i]==nums[i-1]) continue;
int begin=i+1,end=len-1;
while(begin<end){
sum=nums[i]+nums[begin]+nums[end];
if(sum==0){
List<Integer> tem=new ArrayList<Integer>();
tem.add(nums[i]);
tem.add(nums[begin]);
tem.add(nums[end]);
result.add(tem);
begin++;end--;
while(begin<end&&nums[begin]==nums[begin-1]) begin++;
while(begin<end&&nums[end]==nums[end+1]) end--;
}else if(sum<0){
begin++;
}else end--;
}
}
return result;
}
}
【LeetCode】15. 3Sum 三个数和为0的更多相关文章
- [LeetCode] 15. 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 ...
- [leetcode]15. 3Sum三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- [LeetCode] 15. 3Sum ☆☆☆(3数和为0)
描述 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Fi ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- Leetcode 15. 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 ...
- leetcode 15. 3Sum 双指针
题目链接 给n个数, 找出三个数相加结果为0的所有的组, 不可重复. 用双指针的思想,O(n^2)暴力的找, 注意判重复. class Solution { public: vector<vec ...
- Leetcode 15——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 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
随机推荐
- IntelliJ IDEA添加过滤文件或目录
Settings→Editor→File Types 在下方的忽略文件和目录(Ignore files and folders)中添加自己需要过滤的内容 下图为我自己添加过滤的内容,例如:*.iml; ...
- 最小生成树算法(Prim,Kruskal)
边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路.可以 ...
- Hadoop学习4--安装Hadoop
首先献上Hadoop下载地址: http://apache.fayea.com/hadoop/core/ 选择相应版本,点一下,直接进行http下载了. 对原来写的一篇文章,相当不满意,过于粗糙了,于 ...
- Android sqlite管理数据库基本用法
Android操作系统中内置了sqlite数据库(有关sqlite数据库详细介绍见:http://zh.wikipedia.org/wiki/SQLite),而sqllite本身是一个很小型的数据库, ...
- MyEclipse背景色不伤眼+字体大小调节+代码格式化不换行
- PO_PO系列 - 收货管理分析(案例)
2014-07-01 Created By BaoXinjian
- HTML document对象(2)
五.相关元素操作: var a = document.getElementById("id");找到a: var b = a.nextSibling,找a的下一个同辈元素,注意包含 ...
- vim使用快捷键
vim使用快捷键 索引 1. 关于Vim 1.1 Vim的几种模式 2. 启动Vim 3. 文档操作 4. 光标的移动 4.1 基本移动 4.2 翻屏 4.3 标记 5. 插入文本 5.1 基本插入 ...
- JAVA 什么时候使用静态
static所修饰的内容是成员(成员属性.成员方法) 从两方面入手:1.什么时候使用静态的成员属性:当属于同一个类的所有对象出现共享数据时,需要将存储这个共享数据的成员变量用static修饰 2.什么 ...
- Coding 初级教程(一)——用GitHub的GUI客户端对Coding的项目进行管理
一.概述 二.Git基本概念 1.有关存储的四个概念 2.分支(branch) 三.项目管理实战操作 1.安装 GHfW(GitHub for Windows) 2.在Coding上新建一个项目(新建 ...