题意:

  给出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. Objective-C( 语法一)

    点语法 点语法的本质是方法调用 成员变量的作用域 @public : 在任何地方都能直接访问对象的成员变量 @private : 只能在当前类的对象方法中直接访问(@implementation中默认 ...

  2. 使用HttpClient访问被保护资源

    下面的Android应用需要向指定页面发送请求,但该页面并不是一个简单的页面,只有当用户已经登录,而且登录用户的用户名是crazyit.org时才可访问该页面.如果使用HTTPURLConnectio ...

  3. ViewPager滑动页面的实现方法

    package com.lixu.pagerview; import java.util.ArrayList; import android.app.Activity; import android. ...

  4. S1 : 函数

    一.做为值的函数 例如,假设有一个对象数组,我们想要根据某个对象属性对数组进行排序.而传递给数组sort()方法的比较函数要接收两个参数,即要比较的值.可是,我们需要一种方式来指明按照哪个属性来排序. ...

  5. 转: html表单中get方式和post方式的区别

    1.Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据.  2.Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接 ...

  6. svm特征

    svm特征格式:<label><index1>:<value1><index1>:<value1>.... 其中<label> ...

  7. IT公司100题-6-根据上排给出十个数,在其下排填出对应的十个数

    问题描述: 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数要求下排每个数都是先前上排那十个数在下排出现的次数.上排的十个数如下:[0,1,2,3,4,5,6,7,8,9] 举一个例子, ...

  8. C++-理解构造函数、析构函数执行顺序

    先初始化序列中的函数调用,如果基类构造函数为非引用传递,则引起参数的拷贝构造 再: 先类内的成员构造函数(拷贝/默认),再类的构造函数:先基类,再派生类: 本文主要说明对象创建时构造函数的执行顺序,对 ...

  9. CSS盒子模型和IE浏览器

    CSS盒模型图解 下面是一幅关于应用了CSS的元素是如何显示它的尺寸的图示. 在本篇文章中,所有的浏览器在计算盒模型总宽度时处理margin属性的方式都是一致的,所以我们将更多的精力放在padding ...

  10. Program C 暴力求解

    Description   A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...