【一天一道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 ...
随机推荐
- Dynamics CRM build numbers
Dynamics CRM build numbers CRM各大版本及补丁列表,整理的很全
- 解决linux删除文件后空间没有释放问题
linux删除文件后沒有释放空间 今天发现一台服务器的home空间满了,于是要清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [ro ...
- Android适配难题全面总结
支持多种屏幕 Android 可在各种具有不同屏幕尺寸和密度的设备上运行.对于 应用,Android 系统在不同设备中提供一致的开发环境, 可以处理大多数工作,将每个应用的用户界面调整为适应其显示的 ...
- Spark Scheduler模块源码分析之DAGScheduler
本文主要结合Spark-1.6.0的源码,对Spark中任务调度模块的执行过程进行分析.Spark Application在遇到Action操作时才会真正的提交任务并进行计算.这时Spark会根据Ac ...
- JAVA面向对象-----接口的概述
接口的概述 **接口(interface):**usb接口,主要是使用来拓展笔记本的功能,那么在java中的接口主要是使用来拓展定义类的功能,可以弥补java中单继承的缺点. class Pencil ...
- memcached实战系列(六)理解Memcached的数据存储方式
Memcached的数据存储方式被称为Slab Allocator,其基本方式是: 1:先把内存分成很多个Slab,这个大小是预先规定好的,以解决内存碎片的问题.启动参数的时候配置进去的不懂得可以参考 ...
- 早期Swift中Cocos2D初始化代码的重构
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在早期的Swift中在子类里只能调用超类的design ...
- windows下安装nginx (转载自:http://blog.163.com/njut_wangjian/blog/static/1657964252013327103716818/)
1. 到nginx官网上下载相应的安装包,http://nginx.org/en/download.html:下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如下图 ...
- C++对C的实用性增强
#include <iostream> using namespace std; //C语言中的变量都必须在作用域开始的位置定义!! //C++中更强调语言的"实用性" ...
- How to Find the Self Service Related File Location and Versions
How to Find the Self Service Related File Location and Versions (文档 ID 781385.1) In this Document ...