【一天一道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 ...
随机推荐
- linux添加环境变量(path)
分为三步 1.sudo vim /etc/profile 2.export PATH="全路径:$PATH" 3.source /etc/profile 我的微信二维码如下,欢迎交 ...
- SQL Server性能优化——等待——SLEEP_BPROOL_FLUSH
前言: 有一个用于历史归档的数据库(简称历史库),经过一定时间的积累,数据文件已经达到700多GB,后来决定某些数据可以不需要保留,就把这部分数据truncate了,空余出600多GB的空间,也就是说 ...
- Hibernate缓存集成IMDG
1 第三方缓存插件 除了Ehcache这种轻量级的缓存方案外,几乎所有IMDG产品都提供了对Hibernate二级缓存的直接支持,常用的有: Ø Hazelcast Ø GridGain Ø J ...
- Retrofit2.0 ,OkHttp3完美同步持久Cookie实现免登录(二)
原文出自csdn: http://blog.csdn.net/sk719887916/article/details/51700659: 通过对Retrofit2.0的<Retrofit 2.0 ...
- 【并发编程】ThreadPoolExecutor参数详解
ThreadPoolExecutor executor = new ThreadPoolExecutor( int corePoolSize, int maximumPoolSize, long ke ...
- Android开发学习之路--NDK、JNI之初体验
好久没有更新博客了,最近一直在看一个仿微信项目,然后看源码并自己实现下,相信经过这个项目可以让自己了解一个项目中的代码以及种种需要注意的事项.不知不觉中博客已经快要40w访问量,而且排名也即将突破30 ...
- 02基于注解开发SpringMVC项目(jar包,异步,request,参数传递,多选的接收,Model传参,map传参,model传参,ajax,重定向,时间日期转换)
1 所需jar包 项目结构如下: 2 web.xml配置文件的内容如下: <?xmlversion="1.0"encoding="UTF-8"?&g ...
- Maven创建EJB
开发工具: eclipse mars wildfly jdk8 maven 右键新建project,选择other 勾选create simple project 填写信息(自行填写),完成后右键项目 ...
- Oracle dblink详解
database link概述 database link是定义一个数据库到另一个数据库的路径的对象,database link允许你查询远程表及执行远程程序.在任何分布式环境里,databas ...
- div效果很好的遮盖层效果
[html] view plaincopyprint? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...