*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 ...
随机推荐
- Educational Codeforces Round 3 A
A. USB Flash Drives time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Linux系统调用接口添加简单示例
1. Linux体系结构 Linux系统的地址空间分为用户空间和内核空间,通过系统调用和硬件中断能够完成从用户空间到内核空间的转移. 2. 系统调用接口 ① 一般情况下,用户进程不能访问内核空间.Li ...
- 搭建一个wordpress网站需要做哪些工作
今天做了自己的个人网站:二飞日志 之前因为服务器的问题,因为备案的原因辛辛苦苦做的站点数据没了.还好的是没有多少数据.没关系,重新来.有了上一次的经验,这次搭建起来比较顺手.但是也出现了几个问题.下面 ...
- bios-----> grub
系统有两块硬盘, 第一块安装的win7, 第二块安装ubuntu 默认从sda加载grub 如果在bios页面选择从sdb启动,会找不到grub 进入原来的sda系统, grub-install / ...
- spring session使用日志
请求进入和退出时,filter触发session的提取和保存 getAttribute时会查询数据库 setAttribute会保存到数据库 daemond线程定时自动删除过期的session s ...
- 报错:The valid characters are defined in RFC 7230 and RFC 3986
访问 spring boot controller时,报错:The valid characters are defined in RFC 7230 and RFC 3986 1.特殊符号 @Spri ...
- 解决ifconfig没有网卡问题
ifconfig -a root@kali:~# ifup eth0 ifup: unknown interface eth0 vim /etc/network/interfaces #自行添加网卡 ...
- Web前端常见问题
一.理论知识 1.1.讲讲输入完网址按下回车,到看到网页这个过程中发生了什么 a. 域名解析 b. 发起TCP的3次握手 c. 建立TCP连接后发起http请求 d. 服务器端响应http请求,浏览器 ...
- vue自定义指令拖动div
钩子函数一个指令定义对象可以提供如下几个钩子函数:bind:只掉用一次,指令第一次绑定到元素是调用,在这里可以进行一次性的初始化设置inserted:被绑定元素插入父节点时调用(仅保证父节点存在,但不 ...
- tcp中 fast_open 学习 nginx 13年的版本开始支持该功能
https://www.cnblogs.com/lanjianhappy/p/9868622.html 三次握手的过程中,当用户首次访问server时,发送syn包,server根据用户IP生成coo ...