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 ...
随机推荐
- mysql模糊查询实践总结
%代表任意多个字符 _代表一个字符 在 MySQL中,SQL的模式缺省是忽略大小写的 正则模式使用REGEXP和NOT REGEXP操作符. “.”匹配任何单个的字符.一个字符类 “[...]”匹配在 ...
- Thymeleaf添加页面模板
在页面编辑中,有一些公共的页面部分(比如header,footer等)可以抽取出来放到模板中,其他页面使用的时候可以调取该模板,这样的好处是如果修改这些公共部分,只修改模板中的代码即可. 为了做到这一 ...
- use html5 video tag with MSE for h264 live streaming
本编博客记录桌面虚拟化移动端预研. 完整demo: https://github.com/MarkRepo/wfs.js 常见的直播方案有RTMP RTSP HLS 等等, 由于这些流都需要先传输到服 ...
- centos7 vmware克隆解决网络问题
centos7克隆后,发现两台机子之间的MAC相同,无法获取IP.超找资料,解决办法如下: 只要删除网卡的配置文件中的HWADDR和UUID两行就行.使用ifup启动网卡即可.
- oracle external密码验证
什么是external密码验证 当OS user 中存在和DB user 同名的用户时 直接使用和DB user 同名的OS user 可以不输入密码直接登录数据库, [oracle@zxrac1 ...
- go——工程结构
Go是一门推崇软件工程理念的编程语言,它为开发周期的每个环节都提供了完备的工具和支持. Go语言高度强调代码和项目的规范和统一,这几种体现在工程结构或者说代码体制的细节之处. 1.工作区 一般情况下, ...
- PAT 天梯赛 L1-003. 个位数统计
题目链接 https://www.patest.cn/contests/gplt/L1-003 题意 计算每个不同数字出现的次数 思路 可以用 MAP标记, 也可以直接用数字 存 AC代码 #incl ...
- Kotlin学习记录3
参考我的博客:http://www.isedwardtang.com/2017/09/04/kotlin-primer-3/
- Java 基础总结(二)
本文参见:http://www.cnblogs.com/dolphin0520/category/361055.html 1. 字节流与和字符流 1). 字符流操作时使用了缓冲区,而在关闭字符流时会强 ...
- React Native之持久化存储(AsyncStorage、react-native-storage)的使用
AsyncStorage是一个简单的.异步的.持久化的Key-Value存储系统,它对于App来说是全局性的.这是官网上对它的介绍.可以知道,这个asyncstorage也是以键值对的形式进行存储数据 ...