【leetcode】15. 3 Sum 双指针 压缩搜索空间
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets.
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res={};
if(nums.size()<3) return res;
// 暴力发 o(n3) cn/3 如何抽取这三个元素
// 如何保证插入的元素 不重复 多用set
// 双指针的改进版本 这个题目 有点意思
sort(nums.begin(),nums.end());// 压缩搜索空间
set<vector<int>> dp;
int n=nums.size();
for(int i=0;i<n;++i){
int left=i+1;
int right=n-1;
while(left<right){
if((nums[i]+nums[left]+nums[right])==0){
dp.insert({nums[i],nums[left],nums[right]});
left++;
right--;
}
else if((nums[i]+nums[left]+nums[right])<0){
left++;
}
else if((nums[i]+nums[left]+nums[right])>0){
right--;
}
}
}
for(auto dd:dp){
res.push_back(dd);
}
return res;
}
};
【leetcode】15. 3 Sum 双指针 压缩搜索空间的更多相关文章
- LeetCode#15 | Three Sum 三数之和
一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] 1. Two Sum 两数和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- cf22A Second Order Statistics(STL-UNIQUE的使用)
题意: N个数,找出第二大的数.如果没有输出-1. 思路: UNIQUE的使用. 代码: int a[105]; int n; int main(){ cin>>n; rep(i,0,n- ...
- Cesium实现右键框选
思路 1.先取消地图的右键事件 2.右键框选事件,屏幕坐标转为经纬度坐标 取消地图的右键事件 //此处容易犯一个错误:以为右键事件绑定了缩放功能,伪代码即 Cesium.MouseEvent.右键事件 ...
- Pycharm的安装简介
Pycharm 1. Pycharm简介 PyCharm是由JetBrains打造的一款Python IDE,VS2010的重构插件Resharper就是出自JetBrains之手.同时支持Googl ...
- Linux环境下安装中山大学东校区iNode客户端
在中山大学登录校园网有两种方式,一种是连接WiFi,另一种是连接网线.这两种上网方式都需要用到NetID,但是连接网线的话还需要使用到iNode客户端(指东校区). Windows下iNode客户端的 ...
- GDB 调试技巧(不断更新中......)
一.break到不同类的同名函数 方法: 在函数前面加类名以及作用域运算符 eg : break A::func //break 到类A的func函数 程序如下: //gdb_test.cpp #in ...
- mapper接口绑定异常
前言 由于MP的代码生成器把mapper接口绑定的写sql语句xml文件创建在java目录下,而Maven加载机制只会将.java文件编译成.class文件,所以在target目录下找不到写xml文件 ...
- CentOS8安装VirtualBox,并创建CentOS虚拟机
安装VirtualBox 执行以下命令并启用VirtualBox和EPEL包仓库 [root@localhost~] dnf config-manager --add-repo=https://dow ...
- 第三天 while循环 及其用法
(1)语法格式:while 条件: ..... 语法二:while 条件: break # 跳出当前循环 语法三:while 条件: else # 当while循环正常结束时执行该语句:只有程 ...
- ES访问遇到sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
cmd命令cd到jre/bin目录下 输入命令keytool -import -alias 别名 -keystore cacerts -file C://certs//elasticsearch// ...
- Android-ION内存管理简介
ION内存管理简介 https://www.jianshu.com/p/4f681f6ddc3b http://kernel.meizu.com/memory%20management%20-%20i ...