【Permutations】cpp
题目:
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的更多相关文章
- 【Subsets】cpp
题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...
- 【Anagrams】 cpp
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- 蓝桥杯 【dp?】.cpp
题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...
- 【Triangle 】cpp
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- 【N-Queens】cpp
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 【Candy】cpp
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- 【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 = ...
- 【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 ...
随机推荐
- nginx负载均衡配置一(反向代理)
一.前提 1:系统linux(centos) 2:nginx代理服务器(web:192.168.1.10 proxy.abc.com) 3:nginx后台服务器(web1:192.168.1.11 ...
- sqoop的codegen工具
一.codegen工具的使用 sqoop codegen --connect jdbc:mysql://localhost:3306/test --username root --password 1 ...
- input实时监听(input oninput propertychange onpropertychange)
本文实例讲述了js与jquery实时监听输入框值的oninput与onpropertychange方法.分享给大家供大家参考.具体如下: 最近做过一个项目,需求是下拉框里自动匹配关键字,具体细节是实时 ...
- 纯js拖拽参考
function myDrag(obj){ obj.onmousedown=function(e){ var e=e||window.event; var diffX=e.clientX-this.o ...
- svn的使用!!!
1.SVN:subversion(子级源代码版本控制管理软件) 2.SVN的作用 (1)避免开发同一项目不会出现代码覆盖. (2)同一文件可以创建许多不同的版本,并可以随时查看不同版本的内容. (3) ...
- delphi构造&析构调用顺序
_ClassCreate ->Create ->AfterConstruction(->DoCreate / OnCreate) BeforeDestruction(->DoD ...
- java android 访问DELPHI 的DATASNAP
最新版的DELPHI开发DATASNAP非常简单便捷,DataSnap的REST风格和对JSON的支持,使之成为服务器端开发的神器. 一.DATASNAP服务器中的方法: TServerMethods ...
- 09-排序2 Insert or Merge
要点就是把排序每一步,判断一下是否和第二组数据相同,若相同则输出排序方法和下一次序列. According to Wikipedia: Insertion sort iterates, consumi ...
- openstack命令
整理了Openstack命令: openstack aggregate add host openstack aggregate createopenstack aggregate deleteope ...
- Unicode字符以16进制表示
int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个 ...