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: [ ...
随机推荐
- lucene底层数据结构——底层filter bitset原理,时间序列数据压缩将同一时间数据压缩为一行
如何联合索引查询? 所以给定查询过滤条件 age=18 的过程就是先从term index找到18在term dictionary的大概位置,然后再从term dictionary里精确地找到18这个 ...
- chrome密码管理
chrome://settings/passwords ------------------------------- [系统盘]:\Documents and Settings\[用户名]\Loca ...
- Java DES 加解密文件
import com.mchange.v2.io.DirectoryDescentUtils; import javax.crypto.Cipher;import javax.crypto.Ciphe ...
- 解决extjs grid 不随窗口大小自适应的问题
解决extjs grid 不随窗口大小自适应的问题 August 30, 2010 zhai Javascript 8,403 viewsGo to comment 最近遇到的问题,在使用grid的时 ...
- Spring mvc中使用request和response
@ResponseBody @RequestMapping(value="/ip", method=RequestMethod.GET) public String getIP(H ...
- 利用开源框架Volley来下载文本和图片。
Android Volley是Android平台上很好用的第三方开源网络通信框架.使用简单,功能强大. 下载连接地址:http://download.csdn.net/detail/zhangphil ...
- 转: html表单中get方式和post方式的区别
1.Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据. 2.Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接 ...
- Blackhat EU 2013 黑客大会(Full Schedule for Black Hat USA 2013)
大会文档下载:https://www.blackhat.com/eu-13/archives.html 此次BH EU 议题整体较水,涉及系统安全.移动安全.网络传输安全.WEB安全.游戏安全等.下面 ...
- 开通了cnblogs
受够了百度空间,换个地方,或许会更好. 以后有机会会将百度空间你的文章搬过来的.
- 可以用WMI来获取磁盘及分区编号
{$APPTYPE CONSOLE} uses SysUtils, ActiveX, ComObj, Variants; function ListDrives : string; var FSWbe ...