【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 ...
随机推荐
- [译]git config
git config git config命令用来设置git的一些配置(包括全局配置和针对单个仓储的配置).git config命令能定义一个仓储的用户信息和用户偏好. 用法 git config u ...
- WCF--提示:异常消息为“传入消息的消息格式不应为“Raw”。此操作的消息格式应为 'Xml', 'Json'。
Ajax调用WCF服务... 本来用 // $.getJSON("http://localhost:16625/Service1.svc/Article_GetInfo", { } ...
- 题目1373:整数中1出现的次数(从1到n整数中1出现的次数)
题目1373:整数中1出现的次数(从1到n整数中1出现的次数) 题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU发来求助信,希望亲们能帮帮他 ...
- Effective Java 读书笔记之一 创建和销毁对象
一.考虑用静态工厂方法代替构造器 这里的静态工厂方法是指类中使用public static 修饰的方法,和设计模式的工厂方法模式没有任何关系.相对于使用共有的构造器来创建对象,静态工厂方法有几大优势: ...
- HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第 ...
- Inorder Successor in Binary Search Tree
Given a binary search tree (See Definition) and a node in it, find the in-order successor of that no ...
- web安全及防护
本文将对web方面的安全问题以及相应的防护方法进行一个简单的介绍. SQL注入(SQL Injection) 原理:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺 ...
- PHP一句话过狗、卫士、D盾等免杀思路!
原文转载于:http://www.legendsec.org/1701.html 觉得写得还算蛮科普的. 00x1.关键字拆分. 比如assert,可以写成 ‘a’.’ss’.’e’. ...
- Tunna内网转发
Tunna和reduh原理一样.. 使用方法: 上传源码包中文件夹webshell下的脚本至网站目录 然后本地进行连接上传的webshell即可 python proxy.py -u http://1 ...
- 前端之常用标签和CSS初识
外层div的宽度是100%,就是视口的大小,当视口被拉窄到小于内层div的宽度980px时,比如800px,此时 外层div宽度为800px,内层div宽度依然为980px,而css中只设置了外层di ...