【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 ...
随机推荐
- Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)了,我们可以利用它来抓取远程的数据进行保存哦,下面整理了一些关于urllib使用中的一些 ...
- ASP.NET Web数据控件
ASP.NET Web数据控件 1.数据控件简介 这包括数据源控件和格式设置控件,前者使您可以使用 Web 控件访问数据库中的数据,后者使您可以显示和操作ASP.NET 网页上的数据. 2.数据控件 ...
- 【深入JVM内核—原理、诊断与优化】第2期开课了
[深入JVM内核—原理.诊断与优化]的讲师“葛一鸣”,人称“一哥”,毕业于浙江工业大学,计算机软件与理论专业硕士,是国家认证系统分析师,OCP.2012年出版过<Java程序性能优化>,荣 ...
- Hadoop之Storm其他_pom
1.pom 配置 <dependency> <groupId>org.apache.storm</groupId> <artifactId>storm- ...
- C++调用shell
1.直接采用system() 2.popen http://www.cnblogs.com/xitang/p/4288808.html
- Jquery Ajax调用aspx页面方法
Jquery Ajax调用aspx页面方法 在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过as ...
- windows 下wamp环境1 配置之apache的安装
一.安装apache2.4 打开网站 apachelounge.com https://www.apachelounge.com/ 点击左侧Downloads,然后选择对应的版本,这里选择Apa ...
- IO基础
流向: 输入流:从硬盘到java 程序 (读数据) 输出流:从java 到硬盘 (写数据) 数据类型: 字节流: {用记事本打开,不能读懂,用 字节流} 输入: InputStrea ...
- Unity手游之路<十>自动寻路Navmesh之跳跃,攀爬,斜坡
http://blog.csdn.net/janeky/article/details/17598113 在之前的几篇Blog总,我们已经系统学习了自动寻路插件Navmesh的相关概念和细节.然而,如 ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...