【一天一道LeetCode】#15 3Sum
一天一道LeetCode系列
(一)题目
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.
For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2)
(二)解题
这道题的关键在于:结果集合中不允许有重复。
想到的方法有两个:1、在查找过程中去重 2、在结果中去重
经过试验,结果中去重会直接超时。
1、过程中去重版本
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> vecresult;
std::sort(nums.begin(),nums.end());
int len = nums.size();
if(len <3) return vecresult;
for(int i = 0 ; i < nums.size() ;)
{
if(nums[i]>0) return vecresult;
for(int j = i+1;j<nums.size()-1;)
{
int temp = 0-nums[i]-nums[j];
vector<int>::iterator iter = find(nums.begin()+j+1,nums.end(),temp);
if(iter != nums.end())
{
vector<int> tempres;
tempres.push_back(nums[i]);
tempres.push_back(nums[j]);
tempres.push_back(*iter);
vecresult.push_back(tempres);
}
j++;
while(j<nums.size()&&nums[j]==nums[j-1]) ++j;//去重
}
i++;
while(i<nums.size()&&nums[i]==nums[i-1]) ++i;//去重
}
return vecresult;
}
};
2、结果中去重版本(超时)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> vecresult;
std::sort(nums.begin(),nums.end());
int len = nums.size();
if(len <3) return vecresult;
for(int i = 0 ; i < nums.size() ; i++)
{
if(nums[i]>0) return vecresult;
for(int j = i+1;j<nums.size()-1;j++)
{
int temp = 0-nums[i]-nums[j];
vector<int>::iterator iter = find(nums.begin()+j+1,nums.end(),temp);
if(iter != nums.end())
{
vector<int> tempres;
tempres.push_back(nums[i]);
tempres.push_back(nums[j]);
tempres.push_back(*iter);
if(vecresult.size()==0)
{
vecresult.push_back(tempres);
}
else{
vector<vector<int>>::iterator it1 = find(vecresult.begin(),vecresult.end(),tempres);
if(it1 == vecresult.end())//结果中去重
{
vecresult.push_back(tempres);
}
}
}
}
}
return vecresult;
}
};
【一天一道LeetCode】#15 3Sum的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- 每日一道 LeetCode (15):二进制求和
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- [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
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- 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 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
随机推荐
- eclipse properties 插件
eclipse properties 插件安装,分享牛,分享牛原创.eclipse properties 编辑器使用. eclipse因为是原生的,可能集成的插件不多,需要自己手动安装.eclipse ...
- Android TV开发总结(二)构建一个TV Metro界面(仿泰捷视频TV版)
前言:上篇是介绍构建TV app前要知道的一些事儿,开发Android TV和手机本质上没有太大的区别,屏大,焦点处理,按键处理,是有别于有手机和Pad的实质区别.今天来介绍TV中Metro UI风格 ...
- springMVC源码分析--SimpleControllerHandlerAdapter(三)
上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...
- Python 函数参数传递机制.
learning python,5e中讲到.Python的函数参数传递机制是对象引用. Arguments are passed by assignment (object reference). I ...
- iOS开发之UIWebView的常见一些用法
虽然现在Xcode8已经开始使用WKWebView这个框架进行网页展示,但是UIWebView也有一些常用的方法需要知道,下面就简单展示一下,仅供大家参考 相关知识:1.设置背景透明:2.加载本地HT ...
- Struts 2 标签库
<s:if>标签 拥有一个test属性,其表达式的值用来决定标签里内容是否显示 <s:if test="#request.username=='clf'"> ...
- Eclipse编写ExtJS卡死问题 eclise js验证取消
1. Eclipse编写ExtJS卡死问题 eclise js验证取消 近期项目用到了extjs,发现项目编译的时候特别的卡,浪费很多时间而且保存的时候还要编译,因此打算看下如何取消验证extjs.最 ...
- 编译GDAL支持MySQL
GDAL支持MySQL需要MySQL的库才可以,编译很简单,修改nmake.opt文件中对应的MySQL的库的路径和lib即可. nmake.opt文件中397行左右,如下: # MySQL Libr ...
- iterm2 快捷键
最近开始使用mac,用iterm2的终端,有些快捷键纪录下. 标签 新建标签:command + t 关闭标签:command + w 切换标签:command + 数字 或者 command + 左 ...
- android 填满手机磁盘空间方法
http://blog.csdn.net/fulinwsuafcie/article/details/9700619 很多时候我们需要进行临界测试. 譬如当手机盘空间存满的条件下应用会有何表现等. 之 ...