LeetCode OJ:Permutations(排列)
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(排列)的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【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 ...
随机推荐
- 生成vuejs项目
生成项目 npm i -g vue-cli > mkdir my-project && cd my-project > vue init webpack npm i ...
- Hurst指数以及MF-DFA
转:https://uqer.io/home/ https://uqer.io/community/share/564c3bc2f9f06c4446b48393 写在前面 9月的时候说想把arch包加 ...
- CoreThink开发(十一)首页控制器判断移动设备还是PC并做相应处理
在home模块Index控制器添加判断代码 application\Home\Controller\IndexController.class.php <?php // +----------- ...
- vue指令详解
一.vue简绍 1. Vue.js是什么 Vue.js也称为Vue,读音/vju:/,类似view,错误读音v-u-e. 版本分为v1.0 和 v2.0 2.Vue.js的特点 1. 是一个构建 ...
- 快速排序算法C语言版
快速排序(Quicksort)是对冒泡排序的一种改进. 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比 ...
- Hadoop2.0中单点故障解决方案总结---老董
Hadoop 1.0内核主要由两个分支组成:MapReduce和HDFS,众所周知,这两个系统的设计缺陷是单点故障,即MR的JobTracker和HDFS的NameNode两个核心服务均存在单点问题, ...
- MySQL 数据备份,Pymysql模块(Day47)
阅读目录 一.IDE工具介绍 二.MySQL数据备份 三.Pymysql模块 一.IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...
- go——反射
反射(reflect)让我们能在运行期探知对象地类型信息和内存结构,这从一定程度上弥补了静态语言在动态行为上地不足.和C数据结构一样,Go对象头部并没有类型指针,通过其自身是无法在运行期获知任何类型相 ...
- oracle procedure简单的将临时表的数据插入或更新到目标表
CREATE OR REPLACE PROCEDURE DEAL_SYNC_SCH_CUSTPHONE_NEW AS CURSOR C_CURU IS SELECT * FROM CBS_COS.SC ...
- service 需要注意的地方
@service标记的class,只能用于标记了@controller的类,用于其他的会出错 mybatis查询查询到返回记录,查询不到返回null