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 = ...
随机推荐
- excle 填充单元格内容到相同长度
=CONCATENATE(REPT("0",4-LEN(H1)),H1)
- Unity 缓冲池概念
在Unity 中碰到一些重复使用的游戏物体时,我们可以使用缓冲池来进行操作,即重复利用
- Odoo-10开发环境配置与测试
Odoo是使用Python写的开源ERP软件,这几年比较火.内部有实施能力的,这个软件还是很不错的.总体来说,国外的这类软件,更多是在做平台(比如微软的AX.SharePoint.SAP等)平台本身具 ...
- JAVA中通过Jaxp操作XML文件基础
Java中有多种方式操作XML文件,目前讲一讲以SUN公司提供的DocumentBuilderFactory工厂类对象操作XML. 使用XML基本操作就是需要CRUD(增删改查),那么首先通过一个查询 ...
- python 学习第一天
第一天接触python,首先感谢老男孩的授课老师!!!! 今天的知识点: 1.首先接触到python的第一个模块getpass(这边有点迷茫,不能确定的是这个getpasss是一个库还是一个模块)ge ...
- TCP笔记
TCP必须是双工通讯 这种通讯方式提高通道利用率,相对于等待确认传输.
- vs关于“当前不会命中断点 还没有为该文档加载任何符号”的解决方法
首先调式的时候确定在debug模式下, 解决方法:工具-选项-调试 -(启用“仅我的代码”)勾去掉.
- Machine.config 文件中节点<machineKey>的强随机生成
Machine.config 文件中节点<machineKey>的强随机生成 <machineKey>这个节允许你设置用于加密数据和创建数字签名的服务器特定的密钥.ASP.NE ...
- pflag如何使用
1 为何我对这个库感兴趣呢? 因为我想看看Kubernetes的源码,Kubernetes,Hugo啥的都是那这个解析的命令行参数 2 安装 go get github.com/spf13/pflag ...
- Lightgbm 随笔
lightGBM LightGBM 是一个梯度 boosting 框架,使用基于学习算法的决策树.它可以说是分布式的,高效的,有以下优势: 更快的训练效率 低内存使用 更高的准确率 支持并行化学习 可 ...