Given an array nums of n integers, are there elements abcin 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:

  1. Given array nums = [-1, 0, 1, 2, -1, -4],
  2.  
  3. A solution set is:
  4. [
  5. [-1, 0, 1],
  6. [-1, -1, 2]
  7. ]
  1. class Solution {
  2. public List<List<Integer>> threeSum(int[] nums) {
  3. List<List<Integer>> ret = new ArrayList<>() ;
  4. if(nums.length==0) return ret;
  5.  
  6. int target;
  7. int len = nums.length-2;
  8. int left; //point to the left side of the array
  9. int right; //point to the right side of the array
  10.  
  11. Arrays.sort(nums);
  12.  
  13. for(int i = 0; i < len; i++){
  14.  
  15. target = 0 - nums[i];
  16. left = i+1;
  17. right = len+1;
  18. if(nums[left] > target) break;
  19.  
  20. while(left < right){
  21. if(nums[left] + nums[right] > target){
  22. right--;
  23. }
  24. else if(nums[left] + nums[right] < target){
  25. left++;
  26. }
  27. else{
  28. List<Integer> ans = new ArrayList<>();
  29. ans.add(nums[i]);
  30. ans.add(nums[left]);
  31. ans.add(nums[right]);
  32. ret.add(ans);
  33.  
  34. //to avoid IndexOutOfBoundsException
  35. left++;
  36. right--;
  37. //for uniqueness
  38. while(nums[left] == nums[left-1] && left < right) left++;
  39. while(nums[right] == nums[right+1] && left < right) right--;
  40. }
  41. }
  42.  
  43. while(nums[i] == nums[i+1]) {
  44. if(i+1 < len) i++; //for uniqueness
  45. else return ret;
  46. }
  47. }
  48. return ret;
  49. }
  50. }

数组问题注意:下标越界

时间复杂度:O(n2),通过两个指针向中间夹逼的方法使得两个数求和的时间复杂度从O(n2)->O(n)。

15. 3Sum (JAVA)的更多相关文章

  1. 15套java架构师、集群、高可用、高可扩展、高性能、高并发、性能优化、Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩展. ...

  2. 15套java互联网架构师、高并发、集群、负载均衡、高可用、数据库设计、缓存、性能优化、大型分布式 项目实战视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展 ...

  3. 15套java架构师大型分布式综合项目实战、千万高并发-视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展 ...

  4. 15套java架构师、集群、高可用、高可扩 展、高性能、高并发、性能优化Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展 ...

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

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

  6. 1. Two Sum&&15. 3Sum&&18. 4Sum

    题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...

  7. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  8. leetcode 15. 3Sum 二维vector

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

  9. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

随机推荐

  1. C#和.Net的关系

    1..net(dot net) .net是一个平台,抽象的平台概念. 实现形式是库:①定义了基本的类型(通用类型系统CTS,common type system).   ②包含.net公共语言运行库( ...

  2. qnx i2c 学习 二

    File still Updating.... many errors have been FOUND , need big change  qnx i2c structure  --written ...

  3. C#,ASP.NET简单的MD5加密,解密

    简单的MD5加密 首先要有一个加解密的规则  就是key 代码如下 // 创建Key public string GenerateKey() { DESCryptoServiceProvider de ...

  4. 拖拽 ‘vue-grid-layout’ 插件了解下

    我接触到vue-grid-layout是通过我们公司的项目,感觉还是比较简单上手的,大概看了有1个小时吧,我是个行动派,就是觉得实践出真知,但是记性也不太好,有时候自己踩过的坑会忘记,会改但是会忘记原 ...

  5. pip升级

    只要出现报错:python -m pip install --upgrade pip.都表示需要进行升级pip版本 查看pip版本:pip -V(pip可能是python2版本或python3版本) ...

  6. LeetCode 86. Partition List 划分链表 C++

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  7. 一些最常见的SNMP的OID自动翻译成zabbix数字进行表示(华为9306)

    转载自:https://blog.51cto.com/davidbj/1173954 随着Zabbix 2.0版本的发布,很多企业开始用zabbix来代替之前的Nagio.Cacti等其它监控软件.至 ...

  8. HTML5 动画用 animation transform transition 做的两个例子

    1,左右移动,自我翻转的圆 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  9. Shell 批量修改主机 用户密码

    问题:132.121.114 和 132.121.118 网段共 48 台主机未添加基础监控,但是 wh 账户不能登录 需进行批量修改密码操作. 目前情况:op1对上述48台机器设备均能免密登录. 操 ...

  10. 如何用jquery获取form表单的值

    $(function(){ $('.btn').click(function(){ alert($('#form').serialize()); }) }) 这样就获取到了 #form的值.