LeetCode OJ-- 二战 Combinations
在1 - 10 中,求出 7 个数的排列组合。
出现了超时,而超时的原因是有好多重复情况,复杂度上来说,和答案的复杂度是一样的,但是答案中重复了太多了,体会下。
超时1:
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ans;
if(n < k || n <= || k <= )
return ans; vector<int> ansPiece;
set<int> checkExist;
for(int i = ; i <= n; i++)
{
ansPiece.clear();
ansPiece.push_back(i);
checkExist.clear();
checkExist.insert(i);
if(k > )
combine(ans,ansPiece,k,n,checkExist);
}
return ans;
}
void combine(vector<vector<int> > &ans, vector<int> &ansPiece, int &k, int &n, set<int> &checkExist)
{
if(ansPiece.size() == k)
{
vector<int> temp = ansPiece;
ans.push_back(temp);
return;
}
for(int i = ; i <=n; i++)
{
if(checkExist.find(i) == checkExist.end())
{
ansPiece.push_back(i);
checkExist.insert(i);
combine(ans,ansPiece,k,n,checkExist);
ansPiece.pop_back();
checkExist.erase(i);
}
}
}
};
超时2:
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ans;
if(n < k || n <= || k <= )
return ans; vector<int> ansPiece;
set<int> checkExist; //记录还没有选的元素
for(int i = ; i <= n; i++)
{
checkExist.insert(i);
} combine(ans,ansPiece,k,checkExist); return ans;
}
void combine(vector<vector<int> > &ans, vector<int> &ansPiece, int &k, set<int> &checkExist)
{
if(ansPiece.size() == k)
{
vector<int> temp = ansPiece;
ans.push_back(temp);
return;
}
set<int>::iterator itr;
for(itr = checkExist.begin(); itr != checkExist.end(); itr++)
{
ansPiece.push_back(*itr);
set<int> check2 = checkExist;
check2.erase(*itr);
combine(ans,ansPiece,k,check2);
ansPiece.pop_back();
}
}
};
正确的:(控制了顺序)
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > ans;
if(n < k || n <= || k <= )
return ans; vector<int> ansPiece;
int start = ;
combine(ans,ansPiece, k,n, start); return ans;
}
void combine(vector<vector<int> > &ans, vector<int> &ansPiece, int &k, int &n, int start)
{
if(ansPiece.size() == k)
{
vector<int> temp = ansPiece;
ans.push_back(temp);
return;
} for(int i = start; i <= n; i++)
{
if(n - i + < k - ansPiece.size())
return;
ansPiece.push_back(i); combine(ans,ansPiece,k,n,i+); ansPiece.pop_back();
}
}
};
LeetCode OJ-- 二战 Combinations的更多相关文章
- LeetCode OJ 77. Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
- LeetCode OJ:Combinations (排列组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
随机推荐
- chroot directory
给 /A/B/C的C目录做chroot,要对C能读写,所以C目录不能做ROOT目录,对B做chroot. 设置C目录所有者为sftp 账户a,组也改为sftp组(这里a和sftp组都是之前建立好的sf ...
- avalon2.2.3发布
avalon2.2.3这次发布带许多好的东西 首先正式有了自己的LOGO 其次有了自己的QuickStart 样例工程, 这个工程整合了路由,表单,表格,切换卡等组件 https://github.c ...
- 怎么评价Facebook的Relay框架?Meteor.js 是什么?
http://www.zhihu.com/question/34531232?rf=34500201 Meteor.js 是什么? 作者:陈天链接:http://www.zhihu.com/quest ...
- JavaScript DOM编程艺术读书笔记(四)
第十章 实现动画效果 var repeat = "moveElement('"+elementID+"',"+final_x+","+fin ...
- UML大战需求分析阅读笔记1
UML这三个字母的全称是Unified Modeling Language,直接翻译就是统一建模语言,简单地说就是一种有特殊用途的语言.你可能会问:这明明是一种图形,为什么说是语言呢?伟大的汉字还不是 ...
- 详解收发不畅原因及U-Mail邮件中继解决之道
邮件在商务往来中扮演着信息交流的重要角色,假如传输受阻,必将造成沟通不畅:可能三五封邮件的投递你意识不到其重要性,但假如长期需和客户保持沟 通,则需要保证其一贯的稳定性,这就很考验相关软件平台的性能是 ...
- WampServer Apache 服务无法启动解决办法
问题:WampServer 安装后mysql服务可以启动,但Apache服务启动不了(前提是已经安装Apache Server) 解决办法: 1.端口冲突,改Apache里httpd.conf中的端口 ...
- Redis基础(转)
ServiceStack.Redis实践 Redis的C#客户端我选择的是ServiceStack.Redis,相比Booksleeve redis-sharp等方案,它提供了一整套从 Redi ...
- Tomcat本地提权漏洞预警(CVE-2016-1240)
Tomcat是个运行在Apache上的应用服务器,支持运行Servlet/JSP应用程序的容器--可以将Tomcat看作是Apache的扩展,实际上Tomcat也可以独立于Apache运行. 漏洞编号 ...
- Huffman编码
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cstri ...