在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的更多相关文章

  1. LeetCode OJ 77. Combinations

    题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...

  2. LeetCode OJ:Combinations (排列组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  4. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  5. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  6. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  7. 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 ...

  8. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. C# 一些知识点总结(二)_路径类,编码类,文件类...

    Path 类:路径类path.GetFileName("文件路径")//获取完整文件名,包括文件名和文件拓展名Path.GetFileNameWithoutExtension(&q ...

  2. 如何在SqlServer中获取前端连接的IP地址,计算机名等信息

    在一些需求中,可能我们需要知道连接到SqlServer的前端程序的一些系统信息,比如前端连接的计算机名称,IP地址,什么时候开始请求连接,什么时候结束连接等信息. 如果你对SqlServer的系统函数 ...

  3. SQL Server中Rowcount与@@Rowcount的用法

    rowcount的用法: rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10select * from 表A 这样的查询只会 ...

  4. [c#]一个窗体调用另一个窗体的事件

    StuAddForm a = new StuAddForm(); a.IfStudent(textBox1.Text.Trim()):

  5. 使用myeclipse为java web项目添加SSH框架

    添加SSH框架时,要严格按照先Struts,再Spring,最后Hibernate.添加方法见下方: 第一步:添加Struts框架 请按照图示一步步认真执行,配置好struts才可以进行下一步 第二步 ...

  6. adb opendir failed ,permission denied

    做数据库的时候,我手机是htc的,root过的,找数据库db文件一直找不到, 我使用的adb命令ls的时候会提示:adb opendir failed ,permission denied ,解决方法 ...

  7. 【NoSql】Redis

    [NoSql]Redis 一. 文档 1. 官网 2. Windows 安装包 3. C# Driver a. ServiceStack.Redis 最新版本是收费的 b. StackExchange ...

  8. UNICODE 7.0定义的表情符

    td.graph { font-size:24px; } UNICODE 7.0定义了78个表情符,从0x1F600到0x1F64F(其中0x1F643和0x1F644没有定义).下表中列出了这些表情 ...

  9. extern关键字

    1.extern "C" void func(){...} extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其 ...

  10. NSMutableAttributedString的使用

    1.设置字符串中数字字符串的颜色: // 设置字符串中数字的颜色 - (void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange: ...