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 ...
随机推荐
- 机器学习PR:k近邻法分类
k近邻法是一种基本分类与回归方法.本章只讨论k近邻分类,回归方法将在随后专题中进行. 它可以进行多类分类,分类时根据在样本集合中其k个最近邻点的类别,通过多数表决等方式进行预测,因此不具有显式的学习过 ...
- php实现只保留mysql中最新1000条记录
这篇文章主要介绍了php实现只保留mysql中最新1000条记录的方法和相关示例及数据库结构,十分的全面,有需要的小伙伴可以参考下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...
- jsp与数据库的连接
经过一段时间的学习与上网查资料,我已经成功的用java语言连接上了数据库, 本以为同理jsp跟数据库的连接肯定水到渠成的,但是在经过尝试很多次后我发现现实永远是骨感的,最终结果是花了一个下午的时间去建 ...
- ndoutils2.2.0(ndo2db)中文乱码问题解决
ndoutils插入中文时,产生数据库乱码请用下面两个文件: 适用版本:ndoutils-2.0.0 数据库初始化mysql.sql: 修改ndoutils-2.0.0/src目录中的db.c ndo ...
- svg绘制圆弧
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Windows 下针对python脚本做一个简单的进程保护
前提: 大家运行的脚本程序经常会碰到系统异常关闭.或被其他用户错杀的情况.这样就需要一个进程保护的工具. 本文结合windows 的计划任务,实现一个简单的进程保护的功能. 利用py2exe生产 ex ...
- centos7安装mariadb10遇到的问题解决
4. 安装中的错误 4.1 /bin/ld: cannot find -lz /bin/ld: cannot find -lzcollect2: error: ld returned 1 exit s ...
- arm_linux_dt
http://www.deyisupport.com/question_answer/dsp_arm/c6000_multicore/f/53/t/44862.aspx
- Oracle 添加字段,并自增
第一步:alter table TOWN add ID int 第二步:Update TOWN set id=rownum; Commit;
- 后缀数组 & 题目
后缀数组被称为字符串处理神器,要解决字符串问题,一定要掌握它.(我这里的下标全部都是从1开始) 首先后缀数组要处理出两个数组,一个是sa[],sa[i]表示排名第i为的后缀的起始位置是什么,rank[ ...