Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

排列组合的问题,不用考虑是否发生重复的情况,代码如下:

 class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
memset(mark, , sizeof(mark));
memset(cdds, , sizeof(cdds));
dfs(, nums.size(), nums);
return ret;
} void dfs(int dep, int maxDep, vector<int> & nums)
{
if(dep == maxDep){
tmp.clear();
for(int i = ;i < maxDep; ++i)
tmp.push_back(cdds[i]);
ret.push_back(tmp);
}else{
for(int i = ; i < maxDep; ++i){
if(!mark[i]){
mark[i] = true;
cdds[dep] = nums[i];
dfs(dep + , maxDep, nums);
mark[i] = false;
}
}
}
}
private:
int cdds[];
bool mark[];
vector<int> tmp;
vector<vector<int>> ret; };

LeetCode OJ:Permutations(排列)的更多相关文章

  1. LeetCode OJ 题解

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

  2. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. [LeetCode] 47. Permutations II 全排列 II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. 【LeetCode OJ】Interleaving String

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

  7. 【LeetCode OJ】Reverse Words in a String

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

  8. LeetCode OJ学习

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

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

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

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

随机推荐

  1. Python 模块之 pyexcel_xls

    一.适用场景 在很多数据统计或者数据分析的场景中,我们都会使用到excel: 在一些系统中我们也会使用excel作为数据导入和导出的方式,那么如何使用python加以辅助我们快速进行excel数据做更 ...

  2. go——数组

    数组(array)就是由若干个相同类型的元素组成的序列. var ipv4 [4]uint8 = [4]uint8(192,168,0,1) 在这条赋值语句中,我们为刚声明的变量ipv4赋值.在这种情 ...

  3. 自己写的一个简单PHP采集器

    自己写的一个简单PHP采集器 <?php //**************************************************************** $url = &q ...

  4. 2016年国内开源maven镜像站点汇总

    本文系转载,原文链接:https://www.cnblogs.com/xunianchong/p/5684042.html 一.站点版 (一).企业站 1.网易:http://mirrors.163. ...

  5. mybatis 插入之后返回id

    usermapper.xml <insert id="insert" parameterType="app.models.User" keyPropert ...

  6. nodejs fs学习

    在本章的开始,我本来只想写一些关于fs模块的内容,虽然这个模块包含的方法非常的多,但没有想到的是它和我们上一篇文章Node.js Buffer还存在着关联,所以我又把关于Buffer的内容简单的学习下 ...

  7. Python面试题之Python对象反射、类反射、模块反射

    python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) 一.getattr 对象获取 class Manager: role = &quo ...

  8. WINDOWS和UNIX换行符的理解

    # WINDOWS和UNIX换行符的理解 **file1.txt**17.143.161.37   其他    美国54.163.255.40   其他    美国 弗吉尼亚州 亚马逊公司 **[ro ...

  9. 介绍Web项目中用到的几款JS日历日期控件和JS文本编辑框插件

    第一款日历日期控件:layDate 官方网站:http://laydate.layui.com/ 第二款日历日期控件:my97 官方网站:http://www.my97.net/ 第三款 文本编辑器控 ...

  10. Book Review of “The practice of programming” (Ⅳ)

    The practice of programming Chapter 4 Interfaces A good programmer should always be good at designin ...