LeetCode Permutations (全排列)
题意:
给出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 (全排列)的更多相关文章
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:全排列【46】
LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
随机推荐
- Oracle存储过程写法
create or replace procedure QIANFEIGL_JIAOKUANDY( cebenh varchar2, kehuh varchar2, hetongh varchar2, ...
- windows下捕获dump
一般要捕获异常只需要两个函数:SetUnhandledExceptionFilter截获异常:MiniDumpWriteDump写dump文件.但是由于CRT函数可能会在内部调用SetUnh ...
- CentOS 6.2下SVN服务器的安装与配置
安装了一下SVN服务器,本文没有与Apache整合,过程如下: 一,下载相关软件: [root@youxia201 test]# wget http://subversion.tigris.org/d ...
- loadrunner 如何做关联
在页面中为了防止CRSF攻击,每次访问登录页面时,在浏览器器端生成一个token. 在提交时检验这个token是否有效,提交后token自动失效. 如果使用loadrunner来测试此系统话需要做 ...
- Eclipse导出可执行Jar文件(包含第三方Jar包)
1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...
- 分批次从musql取数据,每次取1000条
$t = new Gettags(); $num=$t->sum_tag(); $num=$num/1000; $flag_num=ceil($num); $flag_array=array() ...
- POJ 1573 Robot Motion 模拟 难度:0
#define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...
- ZOJ 3329 One Person Game 概率DP 期望 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 本题分数为0的概率不确定,所以不能从0这端出发. 设E[i]为到达成功所 ...
- Problem C HDU 5224
Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows t ...
- Problem B 队列
Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, nu ...