【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 ...
随机推荐
- CentOS7 安装oracle 11g (11.2.0.1.0)
1.安装依赖: #yum -y install binutils compat-libcap1 compat-libstdc++-33 gcc gcc-c++ glibc glibc-devel ks ...
- Python推导式详解,带你写出比较精简酷炫的代码
Python推导式详解,带你写出比较精简酷炫的代码 前言 1.推导式分类与用法 1.1 列表推导 1.2 集合推导 1.3 字典推导 1.4 元组推导?不存在的 2.推导式的性能 2.1 列表推导式与 ...
- vm扩展磁盘容量后不能启动
主要原因是,新添加的磁盘空间没有分配,系统识别不出来,导致不能开机. 解决方法: 找到虚拟机的文件路径地址,默认是C:\Users\用户名\Documents\Virtual Machines\Cen ...
- cwRsync 实现两台服务器Windows Server 2012 R2间的文件同步(备份)
sync下载链接:https://pan.baidu.com/s/1aZeGDA5bU9f1h6nxvVJsDw 提取码:jtv3 1.配置IP地址 Server端:192.167.1.2(自定义) ...
- [后端及服务器][WSL2(Ubuntu)+Docker]从零开始在WSL中安装Docker
目录 简介 WSL 安装 开启虚拟化(BIOS) 检查系统版本 安装WSL 老版本安装详情 简介 想花三篇文章写下从Windows(WSL)上开启Docker部署php/node/vue/html等项 ...
- c++ 中vector 常见用法(给初学者)
c++ 中 vector vector有两个参数,一个是size,表示当前vector容器内存储的元素个数,一个是capacity,表示当前vector在内存中申请的这片区域所能容纳的元素个数. ca ...
- etcd install & configuration
目录 概述 先决条件 相关术语 ETCD 部署 源码安装 其他方式 安装报错 配置文件详解 etcdctl 使用 日志独立 概述 etcd 是兼具一致性和高可用性的键值数据库,为云原生架构中重要的基础 ...
- [loj3075]组合数求和
Subtask1:$m,nd\le 2\times 10^{3}$ 对$M$质因数分解,假设$M=\prod_{i=1}^{k}p_{i}^{\alpha_{i}}$(其中$p_{i}$为素数) ...
- layui增加转圈效果
var loadix = layer.load(1, {shade: [0.1,'#fff']}); layer.close(loadix);
- 力扣 - 剑指 Offer 46. 把数字翻译成字符串
题目 剑指 Offer 46. 把数字翻译成字符串 思路1(递归,自顶向下) 这题和青蛙跳台阶很类似,青蛙跳台阶说的是青蛙每次可以跳一层或者两层,跳到第 n 层有多少种解法,而这题说的是讲数字翻译成字 ...