【leetcode】3Sum (medium)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
思路:
把最小值从头到尾循环,中间值和最大值两边收缩。
我写的时候是在最后去重复,总是超时。后来看了人家的答案,发现可以每次对最小、中间、最大值去重。就可以AC了
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > ans;
if(num.size() < )
{
return ans;
}
int small = ;
int big = num.size() - ;
int mid = ;
int sum = ;
sort(num.begin(), num.end());
for(small = ; small < num.size() - ; /*small++*/) //最小数字循环 中间与最大数字两边收缩
{
mid = small + ;
big = num.size() - ;
while(mid < big)
{
sum = num[small] + num[mid] + num[big];
if(sum > )
{
big--;
}
else if(sum < )
{
mid++;
}
else
{
vector<int> v;
v.push_back(num[small]);
v.push_back(num[mid]);
v.push_back(num[big]);
ans.push_back(v);
do { mid++; }while (mid < big && num[mid - ] == num[mid]); //注意!!
do { big--; }while (mid < big && num[big + ] == num[big]);//注意!!
}
}
do{ small++; }while (small < num.size() - && num[small - ] == num[small]);//注意!!
}
return ans;
}
}; int main()
{
Solution s;
vector<int> num;
num.push_back(-);
num.push_back(-);
num.push_back(-);
num.push_back(-);
num.push_back(-);
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back(); vector<vector<int>> ans = s.threeSum(num); return ; }
【leetcode】3Sum (medium)的更多相关文章
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 【leetcode】3Sum
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 【leetcode】3Sum Closest(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
随机推荐
- [C++基础]关于对象的创建及内存分配
测试: #include <stdio.h>#include <QDebug> class KPoint{public: KPoint(int x, int y){ nx = ...
- padding/margin/border 理解
- maven之ubutu安装
1.下载地址:http://maven.apache.org/download.cgi 2.安装 将下载后的文件解压到你指定的文件即可,命令如下: tar -xzvf apache-maven-3.0 ...
- 虚拟机安装Ubuntu三种网络模式
VMWare提供三种工作模式桥接(bridge).NAT(网络地址转换)和host-only(主机模式). NAT(网络地址转换) 在NAT模式下,虚拟系统需要借助NAT(网络地址转换)功能,通过宿主 ...
- asp.net的sql防注入和去除html标记的方法
一. // <summary> /// 过滤标记 /// </summary> /// <param name="NoHTML">包括HTML, ...
- BZOJ4439——[Swerc2015]Landscaping
0.题目: FJ有一块N*M的矩形田地,有两种地形高地(用'#'表示)和低地(用'.'表示) FJ需要对每一行田地从左到右完整开收割机走到头,再对每一列从上到下完整走到头,如下图所示 对于一个4* ...
- Codeforces Gym 101138 G. LCM-er
Description 在 \([a,b]\) 之间选择 \(n\) 个数 (可以重复) ,使这 \(n\) 个数的最小公倍数能被 \(x\) 整除,对 \(10^9+7\) 取膜. \(1\leqs ...
- BZOJ 2448: 挖油
Description [0,x]中全是1,其余全是0,每个点有一个权值,求最坏情况下得到x的最小权值. Sol DP+单调队列. 首先就是一个 \(O(n^3)\) 的DP. \(f[i][j]\) ...
- net-snmp源码VS2013编译添加加密支持(OpenSSL)
net-snmp源码VS2013编译添加加密支持(OpenSSL) snmp v3 协议使用了基于用户的安全模型,具有认证和加密两个模块. 认证使用的算法是一般的消息摘要算法,例如MD5/SHA等.这 ...
- html5——canvas画直线
<html> <head> <title>canvas demo</title> </head> <body> <canv ...