【leetcode】Permutations (middle)
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]
.
求没有重复数字的全排列
思路:用的标准回溯法,一次AC
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> ans;
if(num.empty())
{
return ans;
} vector<int> X(num.size());
vector<vector<int>> S(num.size());
int k = ;
S[k] = num; while(k >= )
{
while(!S[k].empty())
{
X[k] = S[k].back();
S[k].pop_back();
if(k < num.size() - )
{
k++;
S[k] = num;
for(int i = ; i < k; i++)
{
vector<int>::iterator it;
if((it = find(S[k].begin(), S[k].end(), X[i])) != S[k].end())
{
S[k].erase(it);
}
}
}
else
{
ans.push_back(X);
}
}
k--;
} return ans;
}
};
网上也有用递归的
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > result; permuteRecursive(num, , result);
return result;
} // permute num[begin..end]
// invariant: num[0..begin-1] have been fixed/permuted
void permuteRecursive(vector<int> &num, int begin, vector<vector<int> > &result) {
if (begin >= num.size()) {
// one permutation instance
result.push_back(num);
return;
} for (int i = begin; i < num.size(); i++) {
swap(num[begin], num[i]);
permuteRecursive(num, begin + , result);
// reset
swap(num[begin], num[i]);
}
}
};
【leetcode】Permutations (middle)的更多相关文章
- 【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】Permutations II (middle)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】876. Middle of the Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【LeetCode】Permutations(全排列)
这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...
- 【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【leetcode】Anagrams (middle)
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
随机推荐
- min-device-pixel-ratio
Devices with -webkit-min-device-pixel-ratio: 2.0 All Macs with Retina displaysApple iPhone 4Apple iP ...
- WCF--安全小见解...
由于WCF写的服务需要Ajax来进行调用(这个配置过程也是一个比较咕~~(╯﹏╰)b的经历...), 所以调用的过程都是前台可以看到的,不加点安全措施上去,真的像是一个裸奔在互联网上的接口... 反正 ...
- ReactiveCocoa源码拆分解析(一)
(整个关于ReactiveCocoa的工程可以在https://github.com/qianhongqiang/QHQReactive下载) ReactiveCocoa的介绍我就不说了,可以自行百度 ...
- windows 下wamp环境1 配置之apache的安装
一.安装apache2.4 打开网站 apachelounge.com https://www.apachelounge.com/ 点击左侧Downloads,然后选择对应的版本,这里选择Apa ...
- ActionBar使用
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...
- 1.xrange和range不要混了,2.range(len(xx))不如用enumerate
range()是列表, xrange()是迭代 >>> a = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i i ...
- ACCESS延时注入
这也算是个新知识吧.今天遇到个站,实实在在存在注入,但是弄不出字段.本应该可以用便宜注入.但是不知道为什么就是就是弄不出来. 大家如果有兴许可以加学习交流群:281245781 交流一下吧. Payl ...
- Sqli-LABS通关笔录-10
好像咋整都没辙.实在是关卡越高越不好整. 弄报错.咋整咋不报错. and sleep(10);鸭蛋的也不好搞.实在没辙.就看源码了. 由代码得出payload: THE END
- JAVA_HOME path classpath 以及cmd编译运行java代码
JAVA_HOME PATH CLASSPATH 三者的区别:安装完jdk之后,首先在环境变量里面添加JAVA_HOME ,例如安装路径为C:\Program Files\Java\jdk1.6.0_ ...
- Python 学习笔记二
笔记二 :print 以及基本文件操作 笔记一已取消置顶链接地址 http://www.cnblogs.com/dzzy/p/5140899.html 暑假只是快速过了一遍python ,现在起开始仔 ...