题目:

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. 1.异步消息Jms及其JmsTemplate的源代码分析,消息代理ActiveMQ

    一. 介绍 借助Spring,有多种异步消息的可选方案,本章使用Jms.Jms的消息模型有两种,点对点消息模型(队列实现)和发布-订阅消息模型(主题). 图1.点对点消息模型(一对一) 图2.发布-订 ...

  2. JavaScript入门基础

    JavaScript基本语法 1.运算符 运算符就是完成操作的一系列符号,它有七类: 赋值运算符(=,+=,-=,*=,/=,%=,<<=,>>=,|=,&=).算术运 ...

  3. 一些peoplecode小技巧【一】

    1. Get the description of the translate value: No need to write SQLEXEC on PSXLATITEM passing fieldn ...

  4. Silverlight中DataPager控件扩展

    大家一定遇到这样的情况,想改变一下SL的DataPager的显示信息,比如希望分页控件上显示数据的总数.那么就需要扩展一下DataPager控件即可. 其实扩展DataPager很简单,只要获取到Da ...

  5. javascript绑定时间 含(IE)

    script language = "javascript" type = "text/javascript"> function test(){ win ...

  6. delphi的多线程编程

    多线程的基本概念 win 98/nt/2000/xp 是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用cpu 运行时间和资源,或者说,把cpu 时间划成片,每个片分给不同的线程 ...

  7. 简单翻译和补充:1. GNU ARM Eclipse

    原文链接: GNU ARM Eclipse GNU 介绍: GNU 计划,又称革奴计划,是由RichardStallman在1983年9月27日公开发起的.它的目标是创建一套完全自由的操作系统.Ric ...

  8. C#条件编译,发布多平台和多种选择性的项目

    http://www.cnblogs.com/chengulv/p/4579528.html 界面操作参考 这样正对不同环境就可以编译出不同的exe或者dll,做到一个项目的灵活多变.条件编译还可以满 ...

  9. 【微网站开发】之微信内置浏览器API使用

    最近在写微网站,发现了微信内置浏览器的很多不称心的地方: 1.安卓版的微信内浏览器底部总是出现一个刷新.前进.后退的底部栏,宽度很大,导致屏幕显示尺寸被压缩 2.分享当前网站至朋友圈时,分享的图片一般 ...

  10. <! [if IE 神奇的条件注释 ]>

    早上起来无聊,看到某学长发的一张代码截图有条件注释,正好,研究一下. 条件注释: 在IE中用来区分IE版本.是否为IE的代码神器! 在其他的浏览器里是不好使的. 不过也值得了,IE都区分出来了,其他的 ...