Leetcode15.3Sum三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]
双指针加去重
class Solution {
public:
vector<vector<int> > threeSum(vector<int>& nums) {
int len = nums.size();
sort(nums.begin(), nums.end());
map<int, pair<int, int> > check;
vector<vector<int> > res;
for(int i = 0; i < len - 2; i++)
{
int low = i + 1;
int high = len - 1;
while(low < high)
{
if(nums[low] + nums[high] == -nums[i])
{
res.push_back({nums[i], nums[low], nums[high]});
//去重
while(nums[low] == nums[low + 1])
low++;
low++;
}
if(nums[low] + nums[high] > -nums[i])
{
high--;
}
else
{
low++;
}
}
//去重
while(nums[i] == nums[i + 1])
i++;
}
return res;
}
};
Leetcode15.3Sum三数之和的更多相关文章
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- [LeetCode] 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 三数之和
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三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- 【LeetCode每天一题】3Sum(三数之和)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- [LintCode] 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 ...
- [Lintcode 3sum]三数之和(python,二分)
题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了, ...
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
随机推荐
- Maven的作用及简介
Maven的作用及简介 一.maven作用 项目之间都是有依赖的,比如A项目依赖于B项目,B项目依赖与C.D项目,等等.这样的依赖链可能很长. 但是,没有一个项目的jar包我们都要导入进去,我们要做的 ...
- 清空标签间的内容(innerHTML)和 value
jquery 方式: 清空标签的innerHTML: $("#divId").html(""); 清空标签的value: $("#divId" ...
- mui.fire()用法
作用: mui.fire() 可以触发目标窗口的自定义事件 mui.fire( 目标窗口的webview , '自定义事件名' ,{参数列表}:) 目标窗口监听这个自定义事件 window.addEv ...
- 2019-5-21-C#-在-构造函数添加-CallerMemberName-会怎样
title author date CreateTime categories C# 在 构造函数添加 CallerMemberName 会怎样 lindexi 2019-05-21 11:28:32 ...
- 刷屏的海底捞超级APP究竟是怎样与阿里云合作的
海底捞正式发布了千人千面超级App已有两月,这家餐饮企业总能带给人们不一样的创新能力.谁能想到25年前从四川起家的火锅店,现在门店遍布国内近100座城市,已开门店超400家,海外门店也有50多家,全球 ...
- JavaSE_08_Collections常用功能
1.1 常用功能 java.utils.Collections是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Colle ...
- IO流 输入和输出文档内容
package io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...
- centos 6.8 搭建禅道 Linux一件安装、进程自起
禅道官网:http://www.zentao.net/ linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅道.Linux 64位一键安装包(适用于L ...
- 洛谷P4244 [SHOI2008]仙人掌图 II
传送门 首先不考虑带环的仙人掌,如果只是一棵普通的树,可以通过dp求每棵子树中的最长链和次长链求树的直径. 那么如果dfs的时候遇到了环,应该用环上的两点挂着的最长链加上两点间的距离来更新树的直径,并 ...
- 阿里面试题,为什么wait()方法要放在同步块中?
某天我在***的时候,突然有个小伙伴微信上说:“哥,阿里面试又又挂了,被问到为什么wait()方法要放在同步块中,没答出来!” 我顿时觉得**一紧,仔细回顾一下,如果wait()方法不在同步块中,代码 ...