题目:

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

代码:

class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int> > ret;
vector<int> none;
ret.push_back(none);
for ( size_t i = ; i < nums.size(); ++i ){
vector<vector<int> > tmp = ret;
ret.clear();
for ( size_t j = ; j < tmp.size(); ++j ){
for ( size_t k = ; k < i+; ++k ){
vector<int> ori = tmp[j];
ori.insert(ori.begin()+k, nums[i]);
ret.push_back(ori);
}
}
}
return ret;
}
};

tips:

参考(http://bangbingsyb.blogspot.sg/2014/11/leetcode-permutations-i-ii.html

采用增量构造法(暴力法解决):每次新增一个元素,对上次已有的permutations,从0到size挨个位置插入一遍。

[]  // 注意一开始要给ret一个空的vector<int> 这样才循环才能run起来

[]

[ 1],[1 ]

[ 2 1], [2 1], [2 1 ], [ 1 2 ], [1 2], [1 2 ]

=======================================

又写了一版DFS的代码,如下:

class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int> > ret;
vector<int> tmp;
vector<bool> used(nums.size(), false);
Solution::perpermute(nums, ret, tmp, used);
return ret;
}
static void perpermute(
vector<int>& nums,
vector<vector<int> >& ret,
vector<int>& tmp,
vector<bool>& used )
{
if ( tmp.size()==nums.size() )
{
ret.push_back(tmp);
return;
}
for ( int i =; i < nums.size(); ++i )
{
if (used[i]) continue;
tmp.push_back(nums[i]);
used[i] = true;
Solution::perpermute(nums, ret, tmp, used);
tmp.pop_back();
used[i] = false;
}
}
};

tips:

tmp用于不断构造一个permutation,每一层代表permutation的一个位置,每层递归添加一个元素。

如果判断某个元素是否能被添加到tmp的后面呢?这里的办法是维护一个bool数组:每个位置判断nums对应位置上的元素是否被使用。

dfs终止条件,如果tmp的size已经为整个nums的size了,证明构造出来一个排列,可以返回

===================================================

第二次过这道题,先用暴力增量法写了一个。这个跟subset的方法类似,可以沿用这个套路。

class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int> > ret;
vector<int> none;
ret.push_back(none);
for ( int i=; i<nums.size(); ++i )
{
vector<vector<int> > tmp = ret;
ret.clear();
for ( int j=; j<tmp.size(); ++j )
{
for ( int k=; k<tmp[j].size(); ++k )
{
vector<int> curr = tmp[j];
curr.insert(curr.begin()+k, nums[i]);
ret.push_back(curr);
}
vector<int> curr = tmp[j];
curr.insert(curr.end(), nums[i]);
ret.push_back(curr);
}
}
return ret;
}
};

再用dfs写一遍。

class Solution {
public:
vector<vector<int> > permute(vector<int>& nums)
{
vector<vector<int> > ret;
vector<int> tmp;
vector<bool> used(nums.size(), false);
Solution::dfs(ret, nums, used, tmp);
return ret;
}
static void dfs(
vector<vector<int> >& ret,
vector<int>& nums,
vector<bool>& used,
vector<int>& tmp)
{
if ( tmp.size()==nums.size() )
{
ret.push_back(tmp);
return;
}
for ( int i=; i<nums.size(); ++i )
{
if (used[i]) continue;
tmp.push_back(nums[i]);
used[i] = !used[i];
Solution::dfs(ret, nums, used, tmp);
tmp.pop_back();
used[i] = !used[i];
}
}
};

【Permutations】cpp的更多相关文章

  1. 【Subsets】cpp

    题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...

  2. 【Anagrams】 cpp

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  3. 蓝桥杯 【dp?】.cpp

    题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...

  4. 【Triangle 】cpp

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  5. 【N-Queens】cpp

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  6. 【Combinations】cpp

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  7. 【Candy】cpp

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  8. 【4Sum】cpp

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  9. 【3Sum】cpp

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

随机推荐

  1. C++ sstream 中处理字符串

    C++引入ostringstream.istringstream.stringstream这三个类,要使用他们创建对象就必须包含<sstream>这个头文件. istringstream的 ...

  2. JS创建对象的方式

    1.采用直接量创建方式:系统会使用new方式自动创建对象 var o = {x:1,y:2,z:2}; 2.采用new关键字创建对象:采用构造函数创建对象 var o = new Object();/ ...

  3. POJ C++程序设计 编程题#3 编程作业—文件操作与模板

    编程题#3: 整数的输出格式 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 利 ...

  4. 操作MySQL数据库

    向表中插入数据 insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下: insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, ...

  5. Jquery数组操作技巧

    Jquery对数组的操作技巧. 1. $.each(array, [callback]) 遍历[常用]  解释: 不同于例遍 jQuery 对象的 $.each() 方法,此方法可用于例遍任何对象(不 ...

  6. python 解析XML python模块xml.dom解析xml实例代码

    分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...

  7. IOS学习2

    1. #import,#include 和@class的区别 都引用一个类,根本定义区别:#include ,#import会把所有的copy一份到该文件 #import比#include的优势,im ...

  8. Python多版本安装 Python2.7和Python3.5

    声明:本文仅仅在win8.1测试通过! 1.下载 Python2.7,3.5 2.依次安装Python27(c:\Python27)  Python35(c:\Python35) 3.c:\Pytho ...

  9. 【原创】可以换行的RadioGroup

    0.效果截图: 以上两个RadioGroup均使用FNRadioGroup实现. 1.控件代码: public class FNRadioGroup extends ViewGroup { /** 没 ...

  10. Debug.print的用法

    使用Debug.print可以用来更好的调试VBA程序 通过ALT+F11代开VBA编程窗口) 插入模块,接着在窗口中输入以下代码,按F5执行 Sub Excute() Debug.Print * + ...