*15. 3Sum (three pointers to two pointers), hashset
Given an array nums of n integers, are there elements a, b, c in 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]
]
Idea: Simplify this problem from O(n^3) to n square by using two pointers
iterate i and j = i+1 and k = n-1 (j and k are two pointers we would use)
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<List<Integer>>();
//two pointers
//set i and move j and k (if sum <0 j++ else k--)
for(int i = 0; i<nums.length; i++){
if (i != 0 && nums[i] == nums[i - 1]) continue; //*****
int j = i+1;
int k = nums.length-1;
while(j<k){
if(nums[i] + nums[j] + nums[k] == 0){
List<Integer> temp = new ArrayList<Integer>();
temp.add(nums[i]); temp.add(nums[j]);temp.add(nums[k]);
//if(!res.contains(temp)) //why add this make TLE ****
res.add(temp);
++j;
//System.out.println("wei");
while (j < k && nums[j] == nums[j-1]) ++j; ****
}else if(nums[i] + nums[j] + nums[k] < 0){
j++;
}else {
k--;
}
}
}
return res;
}
}
1.avoid the duplicate elements -1 -1 (for the same values, there are same results)
2. avoid using contains because of O(n), that is the reason why we need check the duplicate elements manually instead of using contains
Solution 2: using hashmap: n^2*lgn
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(nums);
for(int i = 0; i < nums.length; i++){
if(i!=0 && nums[i-1] == nums[i]) continue;
Set<Integer> set = new HashSet<>(); // no duplicate elements
for(int j = i+1; j<nums.length; j++){// nums[j] : b and c means: count all nums[i] as c
if(set.contains(-nums[i]-nums[j])){ // c
List<Integer> temp = new ArrayList<>();
temp.add(nums[i]);temp.add(nums[j]);temp.add(-nums[i]-nums[j]);
res.add(temp);
//avoid the duplicate elemnts
++j;
while(j < nums.length && nums[j-1]==nums[j]) j++;
--j;
}
if(j<nums.length)
set.add(nums[j]);
}
}
return res;
}
}
hashset
how to using two loop to represent three numbers.
1. treat all nums[j] as c(the third elemnts)
2. As a+b+c = 0, c = -a-b, we need find a and b to satisfy the requirement
Great reference: https://fizzbuzzed.com/top-interview-questions-1/#twopointer -- speak human language nice.
*15. 3Sum (three pointers to two pointers), hashset的更多相关文章
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- 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 ...
- 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 = ...
- 刷题15. 3Sum
一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...
- LeetCode 15. 3Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- [leetcode]15. 3Sum三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
随机推荐
- TCP的粘包问题
什么是粘包 粘包指的是数据与数据之间没有明确的分界线,导致不能正确读取 应用程序无法直接操作硬件,应用程序想要发送数据则必须将数据交给操作系统,而操作系统需要同时为所有应用程序提供数据传输服务,也就意 ...
- FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。
官网:http://ffmpeg.org 一.FFmpeg安装 安装依赖包: yum install libtheora-devel libvorbis-devel 1 FFmpeg编译安装: 由于系 ...
- spring mvc 自定义handler不拦截静态资源
处理静态资源的handler和处理Controller请求中的handler不同,对应的Interceptor也不同 查找对应handler是在DispatcherServlet中 因此一些自定义的I ...
- centos7安装与卸载软件
安装 yum install 服务名 查看服务名 rpm -qa |grep -i aerospike 或者 yum list installed | grep aerospike 卸载 yum re ...
- ubuntu14.04下 安装matlabR2015b遇到的一些问题及其解决方法
问题1:错误提示关于未取得权限,不能再/crack/bin文件中复制文件到安装matlab的/usr/bin文件中? 采取解决方法: 再终端里输入 sudo nautilus,在弹出文件夹里即可进行相 ...
- 解决哈希(HASH)冲突的主要方法
https://blog.csdn.net/xtzmm1215/article/details/47177701 虽然我们不希望发生冲突,但实际上发生冲突的可能性仍是存在的.当关键字值域远大于哈希 ...
- my.ZC
1.100级,裸身,满技能,属性模拟 数据: 大唐 方寸 化生 龙宫 普陀 地府 狮驼 魔王 气血 1200 1900 2600 1200 2600 2600 1900 1900 魔法 7 ...
- ES6数组新增方法总结
关于数组中forEach() .map().filter().reduce().some().every()的总结 let arr = [1, 2, 3, 4, 5] // forEach遍历数组 a ...
- 前端技巧-w3c
1.使用全等===比较符 if (zeroAsAString === 0) { // 判断为false }在和null进行比较的时候,允许使用 == 比较符 2.使用 .parseInt() 的时候, ...
- python 基础内置函数表及简单介绍
内建函数名 (表达形式) 主要作用 备注 abs(x) 返回一个X值得绝对值(x=int/float/复数) all(iterable) 如果 iterable 的所有元素均为 True(或 iter ...