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 双指针 压缩搜索空间的更多相关文章

  1. LeetCode#15 | Three Sum 三数之和

    一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...

  2. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  3. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  4. [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 ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  7. [LeetCode] 1. Two Sum 两数和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  8. 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 ...

  9. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

随机推荐

  1. hdu 3047 Zjnu Stadium(并查集)

    题意: 300个座位构成一个圈. 有N个人要入座. 共有M个说明 :A B X ,代表B坐在A顺时针方向第X个座位上.如果这个说明和之前的起冲突,则它是无效的. 问总共有多少个无效的. 思路: 并查集 ...

  2. $.ajax、$.get和$.post方法成功,完成请求,错误或失败的回调

    一.$.get和$.post的不同    1.get通过url提交的,post是通过http消息实体提交的    2.get提交大小限制为2kb,post不限制    3.get提交会被缓存下来,有安 ...

  3. PHP查看内存占用

    function test(){ echo memory_get_usage(), '<br>'; $start = memory_get_usage(); $a = []; for ($ ...

  4. git stash 存储命令

    应用场景 一.当你接到一个修复紧急 bug 的任务时候,一般都是先创建一个新的 bug 分支来修复它,然后合并,最后删除.但是,如果当前你正在开发功能中,短时间还无法完成,无法直接提交到仓库,这时候可 ...

  5. Ubuntu更换python版本

    Ubuntu更换python版本 ubuntu服务器自带的python版本是python3.6,在运行jwt包时会有版本问题,所以安装和本地相同的python版本=>python3.7 安装py ...

  6. 我個人喜歡的一些Ubuntu的相關配置

    1.vim vim安裝: sudo apt-get install vim-gtk vim美化:刚安装的VIM,可能界面并不是十分友好,我们可以更改vim的配置文件,按照我们的需求去修改它.在命令行下 ...

  7. C/C++ QT QChart 绘制组件应用

    QtCharts 组件是QT中提供图表绘制的模块,该模块可以方便的绘制常规图形,Qtcharts 组件基于GraphicsView模式实现,其核心是QChartView和QChart的二次封装版. 在 ...

  8. Spring中常用注解

    1.@Component 创建类对象,相当于配置<bean/> 2.@Service @Service与@Component功能相同,写在ServiceImpl类上 3.@Reposito ...

  9. 让Qt给你报时,为你读诗词 之 Qt5 TTS

    对,Qt没有食言,9月底如期发布了6.2 LTS.嗯,昨天是9月30日,是月底没错,准时没毛病.博客地址如下 https://www.qt.io/blog/qt-6.2-lts-release 对于老 ...

  10. [cf1491I]Ruler Of The Zoo

    为了统一描述,下面给出题意-- 有$n$只动物,编号为$i$的动物有属性$a_{i,j}$($0\le i<n,0\le j\le 2$) 初始$n$只动物从左到右编号依次为$0,1,...,n ...