题目难度:Medium

题目:

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.

翻译:

给定一个n个整数的数组S,S中是否存在有a b c三个元素使得a+b+c=0?在S中找到所有的非重复组合,使它们的和为0

注意:答案集不能包含重复的数组。例如:[1,5,-6]与[-6,5,1]等数组,答案只出现其中一个

思路:三。。。个。。for循环??

(⊙x⊙;)。。。就要写!

Code:311 / 313 test cases passed.   ——  Time Limit Exceeded      时间复杂度:O(N3)

     public static List<List<Integer>> threeSum1(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> outList = new ArrayList<List<Integer>>();
for (int i = 0; i < nums.length; i++) {
if ((i > 0 && nums[i] == nums[i - 1])) // 各循环起始点不需要判断重复
continue; // 不用i++ 的原因:避免最后的k还要增加边界判断,进入下一个循环则会自动边界判断“i < nums.length”
for (int j = i + 1; j < nums.length; j++) {
if ((j > i + 1 && nums[j] == nums[j - 1]))
continue; // 当有很多for和if的时候,条件取反后用continue,以此取代if的{}缩进,使代码可读性增加
for (int k = j + 1; k < nums.length; k++) {
if ((k > j + 1 && nums[k] == nums[k - 1]))
continue;
if ((nums[i] + nums[j] + nums[k] == 0)) {
List<Integer> inList = new ArrayList<Integer>();
inList.add(nums[i]);
inList.add(nums[j]);
inList.add(nums[k]);
outList.add(inList);
break;
}
}
}
}
return outList;
}

(因为只有在尾巴进行添加并无其他功能,所以采用ArrayList比较实惠和效率)

312测试用例超时:

[82597,-9243,…………此处省略N千个数字]

不想说话,让蠢人静静。。。。

————————————————————————智商分割线————————————————————————

参考答案:74ms——beats85.12%    时间复杂度O(N2)

    public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(nums); for (int i = 0; i < nums.length-2; i++) {
int left = i + 1;
int right = nums.length - 1;
if (i > 0 && nums[i] == nums[i - 1])
continue; // 去掉重复的起点
while (left < right) {
int sum = nums[left] + nums[right] + nums[i];
if (sum == 0) {
result.add(Arrays.asList(new Integer[]{nums[i], nums[left], nums[right]}));
while (left < right && nums[left] == nums[left + 1])
left++; // 去掉重复的左点
while (left < right && nums[right] == nums[right - 1])
right--; // 去掉重复的右点
right--; // 进入下一组左右点判断
left++;
} else if (sum > 0) {
right--; // sum>0 ,说明和过大了,需要变小,所以移动右边指针
} else {
left++; // 同理,需要变大,移动左指针
}
}
}
return result;
}

精髓在于:   (排序 + 去重) +  双指针移动相向定位

注意:不需要“重复的双胞胎数组”,所以是“组合”即Cn/m(从M个数里随机选出N个数有多少种情况)而不是排列An/m——考虑去重

   所查找的数之间具有一定的关联(本题为:和为0),那么就应该利用这个属性,而不是简单的进行搜索。

思路解析:

首先考虑数组遍历时去重:方法一:先将数组排好序,在遍历的时候与上一个进行比较,相同则直接进入下一个

            方法二:用容器Set——简单,但是同样需要排序,增加算法复杂度并且此题三个数操作不方便【在第一题TwoSum的后面有更新用Set的方法】。

            所以数组遍历去重在已排序的情况下优先采取方法一。

其次考虑降低多次循环算法复杂度:

            降低复杂度一般的途径就是利用已有的而未用到的条件将多余的步骤跳过或者删去!

            由于三个数是具有一个特点的:和为某个定值,这个条件只是用来判断了而并没有使用

            并且,由上个去重得知,后面使用的数组是已经排序好的

            此时仔细想想应该就能想到

    从两端使用两个指针相向移动,两端指针所指数之和如果小于目标值,只需要移动左边的指针,否则只需要移动右边的指针!!

     例如[1,1,1,4,5,7,8,8,9]中 定目标值为15,从两边开始,1+9为10,小于15,移动右边指针左移变成1+8只会更少,所以移动左边变成4+9以此类推。。

              妈耶!我想不到啊!

此时为两数定位,而题目中要找3个数

      ………………外嵌for循环!!!又回到了我的领域 ahhhhhhh~

分析搞定!

LeetCode第[15]题(Java):3Sum 标签:Array的更多相关文章

  1. LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  2. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  3. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  4. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  6. LeetCode第[16]题(Java):3Sum Closest 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...

  7. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  8. LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array

    题目难度:Easy 题目: Given a sorted array, remove the duplicates in-place such that each element appear onl ...

  9. LeetCode第[11]题(Java):Container With Most Water 标签:Array

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

随机推荐

  1. 项目实战12.1—企业级监控工具应用实战-zabbix安装与基础操作

    无监控,不运维.好了,废话不多说,下面都是干货. 警告:流量党勿入,图片太多!!! 项目实战系列,总架构图 http://www.cnblogs.com/along21/p/8000812.html ...

  2. Android开发——使用LitePal开源数据库

    前言:之前使用Android内置的数据库,感觉一大堆SQL语句,一不小心就错了,很难受,学习了这个LItePal的开源数据库,瞬间觉得Android内置的数据库简直是垃圾般的存在 LitePal Gi ...

  3. 解决jquery.zclip.js插件无法复制的问题

    网页中需要用到点击复制,在使用webpack加载jquery以及jquery.zclip.js后,出现了以下情况: jquery顺利加载 zclip插件顺利加载 ZeroClipboard.swf顺利 ...

  4. 【python】input、int、if-else、注释、while、module(random.randint())语法示例

    import random luckyNum=random.randint(2,9) i=1 while i<=3: guessNum=input("请你猜猜我的幸运号码:" ...

  5. ES6数组及数组方法

    ES6数组可以支持下面的几种写法: (1)var [a,b,c] = [1,2,3]; (2)var [a,[[b],c]] = [1,[[2],3]]; (3)let [x,,y] = [1,2,3 ...

  6. 使用puppet

    首先配置一个默认文件 只是首次创建的时候才需要重启,后期不需要重启! [root@master manifests]# vim /etc/puppet/manifests/site.pp node d ...

  7. Android Weekly Notes Issue #288

    Android Weekly Issue #288 December 17th, 2017 Android Weekly Issue #288 本期内容主要包括介绍Kotlin DSL使用kotlin ...

  8. Docker容器中开始.NETCore之路

    一.引言 开始写这篇博客前,已经尝试练习过好多次Docker环境安装,.Net Core环境安装了,在这里替腾讯云做一个推广,假如我们想学习.练手.net core 或是Docker却苦于没有开发环境 ...

  9. mysql 证明为什么用limit时,offset很大会影响性能

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/117 首先说明一下MySQL的版本: mysql> sel ...

  10. UVA 11600 Masud Rana

    题目大意:有一个n个点的完全图,有些路上有妖怪.现在一个人从一号点出发,每天随机走向另一个点,消灭路上的妖怪,问平均几天后所有点之间存在没有妖怪的路径.点数≤30. 看到点这么少肯定状压,看见存不下肯 ...