Given an array S of n integers, are there elements abc in S 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.

  1. For example, given array S = [-1, 0, 1, 2, -1, -4],
  2.  
  3. A solution set is:
  4. [
  5. [-1, 0, 1],
  6. [-1, -1, 2]
  7. ]

解法:

  首先对原数组进行排序,然后开始遍历排序后的数组,这里注意不是遍历到最后一个停止,而是到倒数第三个就可以了,中间如果遇到跟前一个数相同的数字就直接跳过。对于遍历到的数,如果大于0则跳到下一个数,因为三个大于0的数相加不可能等于0;否则用0减去这个数得到一个sum,我们只需要再之后找到两个数之和等于sum即可,这样一来问题又转化为了求two sum,这时候我们一次扫描,找到了等于sum的两数后,加上当前遍历到的数字,按顺序存入结果中即可,然后还要注意跳过重复数字。时间复杂度为 O(n2)。代码如下:

  1. public class Solution {
  2. public List<List<Integer>> threeSum(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>(); // 注意不能是new List<>(); List是接口
  4.  
  5. if (nums == null || nums.length < 3) {
  6. return res;
  7. }
  8.  
  9. Arrays.sort(nums);
  10. for (int i = 0; i < nums.length - 2; i++) {
  11. if (nums[i] > 0) {
  12. break;
  13. }
  14. if (i > 0 && nums[i] == nums[i - 1]) {
  15. continue;
  16. }
  17.  
  18. int sum = -nums[i];
  19. int left = i + 1, right = nums.length - 1;
  20.  
  21. while (left < right) {
  22. if (nums[left] + nums[right] == sum) {
  23. ArrayList<Integer> triplet = new ArrayList<>();
  24. triplet.add(nums[i]);
  25. triplet.add(nums[left]);
  26. triplet.add(nums[right]);
  27. res.add(triplet);
  28.  
  29. while (left < right && nums[left++] == nums[left]) {}
  30. while (left < right && nums[right--] == nums[right]) {}
  31.  
  32. } else if (nums[left] + nums[right] < sum) {
  33. while (left < right && nums[left++] == nums[left]) {}
  34.  
  35. } else {
  36. while (left < right && nums[right--] == nums[right]) {}
  37. }
  38. }
  39. }
  40. return res;
  41. }
  42. }

[LeetCode] 15. 3Sum ☆☆的更多相关文章

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

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

  2. leetcode 15. 3Sum 二维vector

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

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

  4. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  5. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  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 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  8. leetCode 15. 3Sum (3数之和) 解题思路和方法

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

  9. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

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

随机推荐

  1. SpringCloud IDEA 教学 (三) Eureka Client

    写在前头 本篇继续介绍基于Eureka的SpringCloud微服务搭建,回顾一下搭建过程, 第一步:建立一个服务注册中心: 第二步:建立微服务并注入到注册中心: 第三步:建立client端来访问微服 ...

  2. mouseover 和 mouseout 事件是可以冒泡的 取消

    mouseover 和 mouseout 事件是可以冒泡的,子元素上触发的事件会冒泡到父元素上.可以改用 mouseleave 和 mouseenter 事件,这两个事件不冒泡.

  3. button type=“submit”

    写js遇到任何怪异的行为 一定要先看看是不是submit搞的鬼. 函数内部最后总是返回 return false; 也是一个好的习惯

  4. 关于char, wchar_t, TCHAR, _T(),L,宏 _T、TEXT,_TEXT、L

    char :单字节变量类型,最多表示256个字符, wchar_t :宽字节变量类型,用于表示Unicode字符, 它实际定义在<string.h>里:typedef unsigned s ...

  5. python学习第二天-基本数据类型常用方法

    1.直入主题 python中基本的数据类型有 数字(整形,长整形,浮点型,复数) 字符串 字节串:在介绍字符编码时介绍字节bytes类型 列表 元组 字典 集合 下面我们直接将以下面几个点进行学习 # ...

  6. C++纯虚函数、虚函数、实函数、抽象类,重载、重写、重定义

    首先,面向对象程序设计(object-oriented programming)的核心思想是数据抽象.继承.动态绑定.通过数据抽象,可以使类的接口与实现分离,使用继承,可以更容易地定义与其他类相似但不 ...

  7. Python ZKPython 安装

    1.由于python客户端依赖c的客户端所以要先安装c版本的客户端cd zookeeper-3.4.5/src/c./configuremake make install 2.下载python扩展包, ...

  8. 【Docker 命令】- exec命令

    docker exec :在运行的容器中执行命令 语法 docker exec [OPTIONS] CONTAINER COMMAND [ARG...] OPTIONS说明: -d:分离模式: 在后台 ...

  9. shiro学习详解(开篇)

    一.前言 要开始接触公司另外一个项目了,RX和我说了下整个项目框架的结构,其中提到权限的控制是通过shiro来处理的,对我而言又是一个全新的知识点,于是今天花了一点时间去学习shiro的使用,看了好几 ...

  10. imshow(A,[])和imshow(A)的区别

    imshow的用法: IMSHOW Display image. IMSHOW(I,N) displays the intensity image I with N discrete levels o ...