题意:

  给出n个元素,请产生出所有的全排列。

思路:

  注意到可能会有相同的排列出现,比如 {2,2}。还有可能是乱序列(大部分情况下都是无所谓的)。

  递归(1):产生的过多的多余vector。

 class Solution {
public:
void recursion(vector<int> num, int i, vector<vector<int> > &res)
{
if (i+==num.size()) res.push_back(num);
else
{
for (int j=i; j<num.size(); j++)
{
if(j!=i&&num[i]==num[j]) continue;
swap(num[i], num[j]);
recursion(num, i+, res);
}
}
}
vector<vector<int> > permute(vector<int> &num)
{
vector<vector<int> >res;
recursion(num, , res);
return res;
}
};

AC代码

   递归(2):只要保证每次交换后都能换回来,必定能恢复到原来的样子,所以不需要产生过多的多余vector。

 class Solution {
vector<vector<int> > ans;
public: void DFS(vector<int>& num,int pos)
{
if(pos+==num.size()) ans.push_back(num);
else
{
for(int i=pos; i<num.size(); i++)
{
swap(num[i],num[pos]);
DFS(num,pos+);
swap(num[i],num[pos]);//换回来
}
}
} vector<vector<int> > permute(vector<int> &num)
{
if(!num.empty()) DFS(num,);
return ans;
}
};

AC代码

  迭代法:类似于BFS,由于不会有重复的数字,所以一个个数来分配。当分配一个数时,只要其前面不会有相同的数,就可以插入到末尾。速度也慢了很多。

 class Solution {
deque<vector<int> > que;
public: bool isok(vector<int>& num,int a)
{
for(int i=; i+<num.size(); i++)
if(num[i]==a) return false;
return true;
} vector<vector<int> > permute(vector<int> &num)
{
que.push_back(vector<int>());
for(int i=; i<num.size(); i++)
{
int siz=que.size();
for(int j=; j<siz; j++)
{
vector<int> tmp(que.front());
que.pop_front();
tmp.push_back();
for(int k=; k<num.size(); k++)
{
if(isok(tmp,num[k]))
{
tmp[i]=num[k];
que.push_back(tmp);
}
}
}
}
return vector<vector<int> >(que.begin(),que.end());
}
};

AC代码

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

  1. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  2. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  3. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  4. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  5. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  6. LeetCode:全排列【46】

    LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...

  7. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

  8. [LeetCode] Permutations II 全排列之二

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

  9. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  10. [leetcode]46. Permutations全排列(给定序列无重复元素)

    Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...

随机推荐

  1. Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-httpAsyncH ...

  2. AngularJS 指令实践

    概述 如果你写过AngularJS的应用,那么你一定已经使用过指令,不管你有没有意识到.你肯定已经用过简单的指令,比如 ng-mode, ng-repeat, ng-show等.这些指令都赋予DOM元 ...

  3. 一模 (4) day1

    第一题: 题目大意:给出N个人之间转账的手续X%,求出A转给B至少要多少钱才能使B得到100元.结果保留8位小数:N<=2000 解题过程: 1.很容易看出这题的图论模型,每条边的权值就是(1- ...

  4. android自定义控件实例(Linearlayout组合TextView和ImageView)

    2013-12-18 11:25:22 转载自: http://www.open-open.com/lib/view/open1328836804515.html 很多时候android常用的控件不能 ...

  5. Windows任务计划

    任务计划,可以将任何脚本.程序或文档安排在某个时间运行.“任务计划”在每次启动windows系统的时候自动启动(默认Task Scheduler服务是开启的)并在后台运行.使用“任务计划”可以完成以下 ...

  6. C# WinForm程序向datagridview里添加数据

    在C#开发的winform程序中,datagridview是一个经常使用到的控件.它可以以类似excel表格的形式规范的展示或操作数据,我也经常使用这个控件.使用这个控件首先要掌握的就是如何向其中插入 ...

  7. VMWare三种工作模式 :bridge、host-only、nat

    VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式.这里 ...

  8. uitabbarcontroller中 在设置tab bar item的image属性后不显示问题

    开始使用ios中的UITabBarController,在给Tab Bar Item设置自定义图片的时候,遇到了问题 按照如下配置: 出来的结果确是: 实际上test24.png应该是: 纠结了很久, ...

  9. LoadImage函数问题

    loadimage函数加载图片类型 Value Meaning IMAGE_BITMAP Loads a bitmap. IMAGE_CURSOR Loads a cursor. IMAGE_ICON ...

  10. oracle 备份和还原还有创建用户、表空间、授权

    --找到存放dbf文件的路径--E:\oracle\product\10.2.0\oradata\orcl--可以通过此语句进行查询select * from v$datafile; --创建表空间c ...