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. POJ 2536 Gopher II(二分图最大匹配)

    题意: N只地鼠M个洞,每只地鼠.每个洞都有一个坐标. 每只地鼠速度一样,对于每只地鼠而言,如果它跑到某一个洞的所花的时间小于等于S,它才不会被老鹰吃掉. 规定每个洞最多只能藏一只地鼠. 问最少有多少 ...

  2. JS基础面试

    1. JS是高级语言弱类型语言 脚本语言 1.1高级语言我们写完的代码不能直接执行,要先经过js引擎翻译成0101这种机器语言才能执行 1.2 弱类型语言变量可以在前一行设置为一个数字,下一行修改为一 ...

  3. DeWeb部署

    DeWeb部署 部署时需要runtime中的大部分文件 需要的目录有: apps,仅包括需要部署的dll即可 dist,必须.请勿改动 media,非必须,一般媒体文件存在于此目录 upload,必须 ...

  4. Win powershell执行策略配置

    参考连接:https://blog.csdn.net/jeffxu_lib/article/details/84710386 参考连接:http://www.cragsman.org/index.ph ...

  5. 大数据SQL中的Join谓词下推,真的那么难懂?

    听到谓词下推这个词,是不是觉得很高大上,找点资料看了半天才能搞懂概念和思想,借这个机会好好学习一下吧. 引用范欣欣大佬的博客中写道,以前经常满大街听到谓词下推,然而对谓词下推却总感觉懵懵懂懂,并不明白 ...

  6. 动态代理中newProxyInstance中三个参数

     JDK Proxy(代理对象): Proxy.newProxyInstance 方法的三个参数创建代理对象 增强 person对象 使用代理对象代替person 去执行 doCourt方法参数1 类 ...

  7. feignclient各种使用技巧说明

    FeignClient常见用法 常规的FeignClient的创建与使用我相信只要使用过spring cloud全家桶的套件的基本上都是非常熟悉了,我们只需定义一个interface,然后定义相关的远 ...

  8. 暑假算法练习Day4

    已经坚持第四天啦,Fighting!!! 1008 数组元素循环右移问题 (20 分) 一个数组\(A\)中存有\(N\)\((>0)\)个整数,在不允许使用另外数组的前提下,将每个整数循环向右 ...

  9. 团队作业3--需求改进&系统

    需求改进&系统设计 这个作业属于哪个课程 软件工程 这个作业要求在哪里 作业要求 这个作业的目标 需求改进&系统设计 目录 需求改进&系统设计 需求&原型改进 针对课堂 ...

  10. 基于Guava API实现异步通知和事件回调

    本文节选自<设计模式就该这样学> 1 基于Java API实现通知机制 当小伙伴们在社区提问时,如果有设置指定用户回答,则对应的用户就会收到邮件通知,这就是观察者模式的一种应用场景.有些小 ...