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:

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

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ret = new ArrayList<>() ;
if(nums.length==0) return ret; int target;
int len = nums.length-2;
int left; //point to the left side of the array
int right; //point to the right side of the array Arrays.sort(nums); for(int i = 0; i < len; i++){ target = 0 - nums[i];
left = i+1;
right = len+1;
if(nums[left] > target) break; while(left < right){
if(nums[left] + nums[right] > target){
right--;
}
else if(nums[left] + nums[right] < target){
left++;
}
else{
List<Integer> ans = new ArrayList<>();
ans.add(nums[i]);
ans.add(nums[left]);
ans.add(nums[right]);
ret.add(ans); //to avoid IndexOutOfBoundsException
left++;
right--;
//for uniqueness
while(nums[left] == nums[left-1] && left < right) left++;
while(nums[right] == nums[right+1] && left < right) right--;
}
} while(nums[i] == nums[i+1]) {
if(i+1 < len) i++; //for uniqueness
else return ret;
}
}
return ret;
}
}

数组问题注意:下标越界

时间复杂度: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. 用最简单的话告诉你什么是ElasticSearch

    介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 L ...

  2. no module named win32api

    1 首先下载pywin32 https://sourceforge.net/projects/pywin32/files/pywin32/ 2进入虚拟环境 D:\env\jdscrapy\Lib\si ...

  3. 微信小程序+没有找到node_modules目录

    第一步:设置-->项目设置-->使用npm模块 第二步:右键目录下miniprogram-->终端打开-->输入npm init-->在packagename下输入:sm ...

  4. Docker容器开机自动启动

     部署项目服务器时,为了应对停电等情况影响正常web项目的访问,会把Docker容器设置为开机自动启动. 在使用docker run启动容器时,使用--restart参数来设置: # docker r ...

  5. html5 video标签屏蔽右键视频另存为的js代码-HTML5教程

    点评:html5 video标签本身有下载功能但是在video区域内,点击右键可以将“视频另存为”下面是屏蔽右键视频另存为的js代码,有此需求的朋友不要错过   做HTML5的video标签,本身我们 ...

  6. 自学大数据(hadoop)小插曲__虚拟机工具

    安装VMware Tools VMware 版本:10.0.1 ubuntu(linux) 版本:16.04 LTS 序言:本来第一天可以访问共享文件夹,第二天重新安装了四个ubuntu,可惜确无法访 ...

  7. 为什么vue支持IE9以上的IE浏览器?

    原因如下: 1.vue框架中核心的双向绑定原理是利用Object.defineProperty()方法实现的. 2.该方法第一个被实现是在IE8中,但是存在诸多限制:只能在DOM对象上使用这个方法,而 ...

  8. upload三种上传方式(上)---Servlet---post---commons-fileupload.1.2.1.jar方式请求上传文件

    上传前进行的配置选项: 1.在下方的Servers中,右键你的tomcat--open,选中下面两个配置. 第一个:Serve modules without publishing 作用:tomcat ...

  9. python大法好——Python 正则表达式

    Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. r ...

  10. azkaban用户管理及权限配置

    参考:https://blog.csdn.net/zlx510tsde/article/details/52287931 官网:https://azkaban.readthedocs.io/en/la ...