Level:

  Medium

题目描述:

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

思路分析:

 求数组中三数和为零的情况,首先我们将数组进行排序,然后遍历数组,设置两个指针left和right,访问到节点i的时候,将left设置为i+1,right设置为nums.length-1。然后看nums[left]和nums[right]的和是否为-nums[i],如果是,那么就找到一组满足要求的解,如果不能则移动left和right,直到相等。

​ 注意要避免重复的情况

代码:

class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>>res=new ArrayList<>();
if(nums==null||nums.length<3)
return res;
Arrays.sort(nums); //进行排序
for(int i=0;i<nums.length;i++){
int towsum=-nums[i];
int left=i+1;
int right=nums.length-1;
while(left<right){
if(nums[left]+nums[right]==towsum){
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
left++;
while(left<nums.length&&nums[left]==nums[left-1])
left++; //避免出现重复情况
right--;
while(right>=0&&nums[right]==nums[right+1])
right--; //避免出现重复情况
}
else if(nums[left]+nums[right]<towsum){
left++;
}
else{
right--;
}
}
while(i<nums.length-1&&nums[i]==nums[i+1])
i++; //避免出现重复情况
}
return res;
}
}

23.3Sum(三数和为零)的更多相关文章

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

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

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

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

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

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

  6. [LintCode] 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. [Lintcode 3sum]三数之和(python,二分)

    题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了, ...

  8. 【LeetCode每天一题】3Sum(三数之和)

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

  9. Leetcode15.3Sum三数之和

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

随机推荐

  1. spring mvc default-servlet mvc:resources mvc:default-servlet-handler区别

    mvc:default-servlet-handler其实就是default-servlet 交由web容器自己处理 mvc:resources spring来处理 没有被映射的url web容器来处 ...

  2. 斐波那契数列,跳台阶(dp思想)

    一 . 斐波那契数列:1,1,2,3,5,8,13,21 即后一项是前两项的和. class Solution { private: ]; public: Solution() { memset(ar ...

  3. Linux常用基本命令 1

    useradd 创建用户. password 修改密码. date 查看时间 man date 帮助文档.f往后翻 b往前翻 q退出.软修改 man hwclock 修改硬件时钟, cal 查看日历 ...

  4. PhoneGap 3.4 开发配置及问题

    PhoneGap这个坑爹货,开发确实迅速,又无需学习新知识,但又有N多深不见底坑,最大的坑无疑是性能,滑动时卡顿明显,iPhone5上性能比较好,大部分安卓上就坑爹了,神马动画效果最好少用:其次是不同 ...

  5. Docker学习之路(一)

    容器简介 管理程序虚拟化(hypervisor virtualization, HV)是通过中间虚拟运行于物理硬件之上.而容器是直接运行在操作系统内核之上用户空间.因此,容器虚拟化运行也成为“操作系统 ...

  6. 11.BETWEEN 操作符

    BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围. BETWEEN 操作符 操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围.这些值可以是数值 ...

  7. 写一段php代码,确保报个进程同时写入同一个文件

  8. Part5核心初始化_lesson4---关闭中断

    1.关闭cpsr寄存器里面的I(中断)和F(快速中断)位: 2.设置中断屏蔽寄存器. 针对2440: 这是中断处理过程,当有中断源(没有子中断源)来的时候,它会把这个中断记录在SRCPND里面:它还要 ...

  9. Servlet请求转发 RequestDispatcher接口.RP

    在Servlet中,利用RequestDispatcher对象,可以将请求转发给另外一个Servlet或JSP页面,甚至是HTML页面,来处理对请求的响应. 一,RequestDispatcher接口 ...

  10. 编译boost,去掉不使用的组件

    说明:下面内容仅针对Linux环境(boost官网为:http://www.boost.org/,可从这里下载它的源代码包,这里要求下载.tar.gz包,而非.7z..zip或bz2包). 在当前目录 ...