题意:

  给出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. 你需要知道的三个 CSS3技巧(转)

    1. 在CSS中用attr()显示HTML属性值 attr()功能早在CSS 2.1标准中就已经出现,但现在才开始普遍流行.它提供了一个巧妙的方法在CSS中使用HTML标签上的属性,在很多情况下都能帮 ...

  2. Android: Intent实现活动之间的交互

    Intent的作用:是Android中各个组件直接交互的一种重要方式,且利用Intent可以启动Activity.Service以及Broadcast Receiver. Intent的创建:显示和隐 ...

  3. php header setcookie headers_sent函数 函数检查 HTTP 标头是否已被发送以及在哪里被发送

    这里需要注意的 header() 最常被拿來送 header('Location: /'); 等等, 做网页重定向的动作. 在使用 setcookie(), header()... 等函数前 不可以用 ...

  4. CentOS 下的MySQL配置

    先贴出代码(/etc/my.cnf)如下: #The following options will be passed to all MySQL clients [client] #password ...

  5. JBoss像tomcat那样创建部署文件,JBoss创建虚拟目录

    jboss可以像tomcat那样,写一个配置文件,指向应用所在的路径,而不用将应用直接复制到deploy下的某一个以.war结尾的文件夹下吗? 答:好像是不能直接操作,但是可以通过变通的方式来搞定.在 ...

  6. jdk、jre、jvm的关系

    JDK里面的工具也是用JAVA编写的,它们本身运行的时候也需要一套JRE,如C:\Program Files\Java\jdk1.5.x\目录下的JRE.而C:\Program Files\Java\ ...

  7. POJ 1436 区间染色

    Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4507   Ac ...

  8. 虚拟机的apache服务器不能被主机访问的问题

    我在centos虚拟机上安装了elasticsearch服务,虚拟机里测试正常,但主机却无法访问elasticsearch.要说的是,虚拟机采用桥接模式,与主机相互ping得通. 后来查了资料发现,这 ...

  9. cssTex

    var head= document.getElementById("head");head.style.cssText="width:200px;height:70px ...

  10. 基于K2 BPM的大型连锁企业开关店选址管理解决方案

    业内有句名言:“门店最重要的是什么?第一是选址,第二是选址,第三还是选址” 选址是一个很复杂的综合性商业决策过程,需要定性考虑和定向分析.K2开关店&选址管理方案重点关注:如何开出更好的店?在 ...