题目:

  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;
}
}

  

Discuss

【LeetCode】15. 3Sum 三个数和为0的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  5. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  6. 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 ...

  7. leetcode 15. 3Sum 双指针

    题目链接 给n个数, 找出三个数相加结果为0的所有的组, 不可重复. 用双指针的思想,O(n^2)暴力的找, 注意判重复. class Solution { public: vector<vec ...

  8. 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 ...

  9. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

随机推荐

  1. Func系列3:自定义模块

    简介 Func自带的模块已经非常丰富,但在日常系统运维当中,尤其是面对大规模的服务器集群.不同类别的业务平台,次是Func自带的模块或许已经不能满足我们的需求,所以有必要通过自定义模块来填补这块的不足 ...

  2. 初步理解MVC

    一.Asp.net WebForms 与Asp.net MVC 概念 Asp.net是创建WEB应用的框架,MVC是能够用更好的方法来组织并管理代码的一种更高级架构体系. 我们可将原来的Asp.net ...

  3. Titan-红号楼宗谱案例

    一. 简介 titan:存储,查询图形结构的数据库.分布式集群环境下,可支持数以千亿级别的点和边,同时支持上千个并发的实时的复杂图形遍历,支持ACID事务. 架构:支持以下3方面的自由组合 (1)节点 ...

  4. PLSQL_性能优化工具系列17_Best Practices: Proactive Data Collection for Performance Issues

    占位符 https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=2082062510193540&id=1366133. ...

  5. DBA_在Linux上安装Oracle Database11g数据库(案例)

    2014-08-08 Created By BaoXinjian

  6. bug_ _fragment的1

    =========  2   fragment小结 ???? ======== 1     fragment:java.lang.IllegalStateException: Can not perf ...

  7. java的枚举

    package com.wzh.test.enum0; import org.junit.Test; public class demo1 { @Test public void testAb(){ ...

  8. xcode 插件管理工具

    每次xcode 更新后之前安装的插件 都失效了!解决办法: 一.可以通过在原插件项目infoplist新增 新xcode的 DVTPlugInCompatibilityUUID 来使之支持新版xcod ...

  9. 要件审判九步法及其基本价值 z

    要件审判九步法及其基本价值 发布时间:2014-12-24 14:29:05 作者介绍 邹碧华,男,1967年出生于江西奉新,毕业于北京大学法学院,获法学博士学位.上海市高级人民法院副院长.2006年 ...

  10. lable标签透明

    方法1: pictureBox1.Controls.Add(lable1);    //或 this.label1.Parent=pictureBox1;   lable1.BackColor=Col ...