3Sum - LeetCode
题目链接
注意点
- 和two sum那道题不一样的是这题返回的是具体的数字,不是下标
解法
解法一:将每个数字都作为target,剩下的数字按照two sum那道题来做,得到的结果先排序然后放进set,保证没有重复的结果。因为用了太多STL容器所以...时间复杂度为O(我也不知道怎么算)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int i,n = nums.size(),target;
set<vector<int>> ansSet;
map<int,int> myMap;
vector<int> anw;
for(i = 0;i < n;i++)
{
target = -nums[i];
int j;
for(j = 0;j < n;j++)
{
if(j != i)
{
if(myMap.count(target - nums[j]))
{
anw.push_back(nums[j]);
anw.push_back(target - nums[j]);
anw.push_back(-target);
sort(anw.begin(),anw.end());
ansSet.insert(anw);
anw.clear();
}
myMap[nums[j]] = j;
}
}
myMap.clear();
anw.clear();
}
vector<vector<int>> ans;
n = ansSet.size();
set<vector<int>>::iterator it;
for(it=ansSet.begin();it!=ansSet.end();it++)
{
ans.push_back(*it);
}
return ans;
}
};
解法二:因为这道题返回的是具体的数字,所以可以直接用sort()先对输入进行排序。然后和解法一一样每个数字都作为target,但是不一样的是,因为是有序的数组所以可以维护双指针加快速度,并且跳过重复的数字加快速度,同时因为跳过了重复的数字,就不需要对结果去重了。时间复杂度O(n^2)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n = nums.size(),target,i = n-1,j,k;
vector<vector<int>> ans;
while(i >= 2)
{
target = -nums[i];
j = 0;
k = i-1;
while(j < k)
{
int temp = nums[j]+nums[k];
if(temp < target)
{
j++;
}
else if(temp > target)
{
k--;
}
else
{
vector<int> v = {nums[i], nums[j], nums[k]};
ans.push_back(v);
j++;
k--;
while(j < k && nums[j-1] == nums[j])
{
j++;
}
while(j < k && nums[k+1] == nums[k])
{
k--;
}
}
}
i--;
while(nums[i+1]==nums[i])
{
i--;
}
}
return ans;
}
};
小结
- 慎用stl容器!不然你不知道你的程序时间复杂度和空间复杂度会变成什么鬼样子
3Sum - LeetCode的更多相关文章
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- 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 un ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 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 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
随机推荐
- PHPCMS增加SEO字段调用
alter table v9_site add site_title_index varchar(255) not null;alter table v9_site add keywords_ind ...
- Ubuntu下配置Anaconda
转自:https://blog.csdn.net/Horcham/article/details/57075388 安装Anaconda Ubuntu下似乎库中不自带Anaconda,是自带纯净的py ...
- [文章存档]Kudu 的 Debug Console 窗口如何查看更多文件
链接:https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-h ...
- hadoop之定制自己的sort过程
Key排序 1. 继承WritableComparator 在hadoop之Shuffle和Sort中,可以看到mapper的输出文件spill文件需要在内存中排序,并且在输入reducer之前,不同 ...
- umount命令详解
基础命令学习目录首页 umount 用来卸载设备 -a:卸除/etc/mtab中记录的所有文件系统: -h:显示帮助: -n:卸除 ...
- [buaa-SE-2017]结对项目-数独程序扩展
结对项目-数独程序扩展 step1~step3:github:SE-Sudoku-Pair-master step4:github:SE-Sudoku-Pair-dev-combine step5:g ...
- Linux 下软件安装
Linux 下软件安装 一.Linux 上的软件安装 通常 Linux 上的软件安装主要有三种方式: 在线安装 从磁盘安装deb软件包 从二进制软件包安装 从源代码编译安装 这几种安装方式各有优劣,而 ...
- Task 9 从用户界面和体验分析“360极速浏览器”
我目前使用的浏览器是360极速浏览器,下面将针对用户界面.记住用户选择.短期刺激.长期使用的好处坏处.不要让用户犯简单的错误四个方面对其进行评估: 1.用户界面: 01 可视性原则--网络没有连接或者 ...
- java 框架 面试 2
1.谈谈你对Struts的理解. 答: 1. struts是一个按MVC模式设计的Web层框架,其实它就是一个大大的servlet,这个Servlet名为ActionServlet,或是ActionS ...
- wamp 修改www目录
我的情况 Wamp版本:2.2 WAMP2.2安装目录:C:/ www目录:D:/wamp/www/ 变更目录:E:/HbuilderProjects/ 一 主要过程: (1)修改 D:\wamp\b ...