在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. {POJ}{3971}{Scales}{O(N)动态规划}

    题意:给定一堆2二进制砝码,给定一个物品,要求在天平两端加入物品和砝码使之平衡,求可能数. 思路:一开始想到了直接用数学原理,结果没证出来.做如下思考,此题需要用二进制: (1)设物品重量为w,加入的 ...

  2. JS的封装

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAl8AAADiCAYAAABwdKKfAAAYLGlDQ1BJQ0MgUHJvZmlsZQAAWIWVeQ

  3. Java入门记(一):折腾HelloWorld

    HelloWorld,学习每门语言的第一步.有人戏称,这些年的编程生涯就是学习各种语言的HelloWorld,不知是自谦还是自嘲.目前所在的公司使用Java作为主要开发语言,我进行语言转换也大半年了, ...

  4. select 选中 option的问题

    1.[可以实现 不推荐  适合多选] $("#organize_type").find("option:eq("+j+")").attr(& ...

  5. google map 计算地图面积方法

    花了几个小时把js的google计算地图面积的算法改成了c# 的. class Program { static void Main(string[] args) { // a = new qq.ma ...

  6. SQL语言

    SQL语言的分类:DDL DML DQL DCL SQL中的操作无非就是(增删改查) DDL:Data Query Language,数据查询语言! 主要是用来定义和维护数据库的各种操作对象,比如库. ...

  7. Python scipy.sparse矩阵使用方法

    本文以csr_matrix为例来说明sparse矩阵的使用方法,其他类型的sparse矩阵可以参考https://docs.scipy.org/doc/scipy/reference/sparse.h ...

  8. Content is not allowed in prolog ---UTF-8 无bom

  9. Newtonsoft.Json高级用法(转)

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

  10. Android WIFI 分析(二)

    本文介绍Wifi 分析线路二:在Setting中打开WiFi功能.扫描网络以及连接网络的流程. WifiSettings 无线网络设置界面 WifiEnabler 相当于无线网络设置开关 WifiDi ...