15. 3Sum (JAVA)
Given an array nums of n integers, are there elements a, b, cin 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)的更多相关文章
- 15套java架构师、集群、高可用、高可扩展、高性能、高并发、性能优化、Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程
* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩展. ...
- 15套java互联网架构师、高并发、集群、负载均衡、高可用、数据库设计、缓存、性能优化、大型分布式 项目实战视频教程
* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展 ...
- 15套java架构师大型分布式综合项目实战、千万高并发-视频教程
* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展 ...
- 15套java架构师、集群、高可用、高可扩 展、高性能、高并发、性能优化Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程
* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展 ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 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 ...
- 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 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- 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 = ...
随机推荐
- Livepeer中文白皮书(翻译)
Livepeer Whitepaper 分布式视频流媒体传输协议及经济激励 Doug Petkanics doug@livepeer.org Eric Tang eric@livepeer.org 翻 ...
- c# datatable按主键合并相同主键返回新的datatable
一.概述: 在python转c#时,python中pandas.merge可以按主键合并两个datatable,苦苦找了很久,希望c#也有同样的函数,未果,就自己写了一个,目前测试没问题,同样我也考虑 ...
- js实现输入某串数字,构建完全二叉树,并判断是否为二叉搜索树
思路:若为二叉搜索树,则中序遍历为递增的 let arr = [15,8,16,6,10];let pindex = [];function Node(){ this.root = null; thi ...
- Android几种视频播放方式,VideoView、SurfaceView+MediaPlayer、TextureView+MediaPlayer,以及主流视频播放器开源项目
简单的说下一Android的几种视频播放功能: 1.VideoView:最简单的视频播放 <FrameLayout xmlns:android="http://schemas.andr ...
- bootstrap-treeview 如何实现全选父节点下所有子节点及反选
转(https://www.augsky.com/992.html) 选中父节点时,父节点下所有子节点也都全部选中 1,HTML代码 <h2>TreeView Checkable</ ...
- java 日志脱敏框架 sensitive-新版本0.0.2-深度拷贝,属性为对象和集合的支持
项目介绍 日志脱敏是常见的安全需求.普通的基于工具类方法的方式,对代码的入侵性太强.编写起来又特别麻烦. 本项目提供基于注解的方式,并且内置了常见的脱敏方式,便于开发. 用户也可以基于自己的实际需要, ...
- win10环境下适应pip安装autobahn提示认证失败的问题
工作需要看一下websocket的东西,然后使用autobahn三方,但是使用pip install安装怎么也安装不上,问了很多网站,都说的升级pip,可是每一个鸡巴管用的,不过还是谢谢他们,现在我只 ...
- js打印页面指定区域,并去掉页眉上的时间和请求路径
需要通过js打印指定页面的内容 <style media=print type="text/css"> .noprint{visibility:hidden} < ...
- (11)ssh免密登录配置
***在Linux命令行中登录到另一台虚拟机(需要用到ssh协议) Linux中默认有ssh的服务器端和客户端,客户端的名字就叫ssh 前提是当前使用的用户名在待连接的虚拟机中存在 格式: ssh ...
- java异常——Exception、RuntimException
一.Exception和RuntimeException的区别 Exception是RuntimeException的父类,使用了 Exception 的类都必须对异常进行处理(try / throw ...