LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++>
给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合。
C++
先自己写了一发,虽然过了,但跑了308 ms...
我的做法是先排序,扫一遍,处理出unorder_map<0-a[i],i>的hash表。再\(O(n^2)\)枚举前两个元素,查表直接知道第三个元素的位置,然后将三元组加入到答案集合中,通过枚举时添加的限制条件规避重复元素。
复杂度\(O(n^2)\),由于使用了hash表,常数较大。
class Solution {
public:
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {
std::vector<std::vector<int> > res;
if(nums.size()<3) return res;
std::sort(nums.begin(),nums.end());
const int target = 0;
std::unordered_map<int,int> ump;
for(auto i=nums.begin();i!=nums.end();i++)
ump[target-*i] = std::distance(nums.begin(),i)+1;
for(auto i=nums.begin();i<nums.end();i++){
if(i>nums.begin() && *i == *(i-1)) continue;
for(auto j=i+1;j<nums.end();j++){
if((j>i+1 && *j== *(j-1))|| ump[*i+*j]-1<=std::distance(nums.begin(),j))continue;
res.push_back({*i,*j,*(nums.begin()+ump[*i+*j]-1)});
}
}
return res;
}
};
学习一发没有常数的\(O(n^2)\),只用了64 ms
排序后,枚举第一个元素,左右指针夹逼(根据枚举元素+左指针元素+右指针元素与0的大小关系,判断移动哪一个指针,移动方向一定是使左右指针所夹范围更小的)
class Solution {
public:
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {
std::vector<std::vector<int> > res;
if(nums.size()<3) return res;
std::sort(nums.begin(),nums.end()); // 先排序
const int target = 0;
auto last = nums.end();
for(auto i = nums.begin();i<last-2;i++){
auto j = i+1; // 左指针初始化
if(i>nums.begin()&&*i == *(i-1)) continue; // 避免枚举重复元素
auto k = last - 1; // 右指针初始化
while (j < k){
if(*i+*j+*k<target){ // 左指针右移,使和变大
j++;
while (*j==*(j-1)) j++; // 跳过重复元素
}
else if(*i+*j+*k>target){ // 右指针左移,使和变小
k--;
while (*k==*(k+1)) k--; // 跳过重复元素
}
else{ // 加入答案集合
res.push_back({*i,*j,*k});
j++;
k--;
while (*j==*(j-1) && *k==*(k+1) && j<k) j++; // 跳过重复元素
}
}
}
return res;
}
};
LeetCode 15 3Sum [sort] <c++>的更多相关文章
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- [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
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- 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 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- 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 ...
随机推荐
- 【题解】狼抓兔子—BZOJ1001。
(胡扯时间)今天炒鸡无聊就打算BZOJ开始从第一道题开始顺着打,这样未来一段时间内也就有事干了.结果发现A+B切掉后就遭遇了一个"小小"的瓶颈(真不友好. 好了说题说题.看题第一眼 ...
- cnpm下载包与npm版本不一致的问题解决
参考链接:https://www.jianshu.com/p/949b4e0ae190
- Urban Elevations UVA - 221
题目大意:给出建筑的俯视图,以及每个建筑的左下角坐标,宽度,长度,高度.求正视图可观察到的建筑的编号 思路:建筑物的可见性等于南墙的可见性,依据左下角排序后,逐个判断每个建筑是否可见.对南墙的x坐标进 ...
- python算法&二分查找法
import random def random_list(n): result = [] ids = list(range(1001,1001+n)) a1 = ["赵",&qu ...
- 关于t,f test
我也是佛了 这么基础的概念其实每次都会搞混一些 首先我们针对variance求一个estimator s 然后对于任意置信区间 (sample mean +- 你所需的置信百分比的t * SE(SE就 ...
- AXI_DMA IP学习
参考:PG201 AXI DMA v7.1 AXI IP核 功能:一旦处理器配置好传输方式之后,DMA可以自己完成内存数据的搬进或者搬出,而不需要处理器的介入.如果使用方法得当,DMA可以显著 ...
- 使用ansible实现轻量级的批量主机管理
作者:邓聪聪 查看ansible配置文件下的hosts的文件 [root@ansible-server scripts]# cat /etc/ansible/hosts [test] 172.16.1 ...
- mingw-gcc-9.0.1-i686-posix-sjlj-201903
-------------------------------------------------------------------------------gcc version 9.0.1 201 ...
- Motivation
觉得一个需求不错,却没有意愿去做,唯一可能的意愿就是生活需要.可这并不能很好的带动起来什么,除了让自己觉得在逼自己. 后来在这个需求的基础上,延伸出新的需求,可能更适应生活.仍然没有意愿去动手,虽然生 ...
- Taro父子组件通信
父组件 testEvent = () =>{ console.log('abc123') } <Test test={1231323} onTestEvent={this.testEven ...