【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 ...
随机推荐
- 介绍一点.NET反编译的知识
反编译是我们理解.NET内部实现的一种良好的手段. 程序编译时 Test.exe是IL代码.我们可以通过一些工具,来查看这些IL代码. 一模一样? 理论上来说,一模一样的反编译是不存在的.原因有以下3 ...
- openstack简介
OpenStack是一个开源的云计算管理平台项目,由几个主要的组件组合起来完成具体工作.OpenStack支持几乎所有类型的云环境,项目目标是提供实施简单.可大规模扩展.丰富.标准统一的云计算管理平台 ...
- 如何循序渐进地学习Javascript
javascript入门太容易了,导致几乎人人随便看看就能上手,零基础的人学个三五天都能对外宣称自己掌握了js.可是真正掌握js是一件很难的事情.如果在初学一门语言的时候第一想到的是问别人,是很难取得 ...
- 从零开始之ecshop基础篇(17)
目标:基于自定义的mvc框架开发的案例(项目) 项目周期 需求分析 典型的业务逻辑: 电子商务:商城(京东),B2C,C2C(淘宝),团购,秒杀,代购 内容管理:新浪门户类,优酷视频管理, ...
- java中4中类修饰符访问范围
public:本类中可见,同包中可见,子类中可见,其他包中可见. protected:本类中可见,同包中可见,子类中可见,其他包不可见. 默认:本类中可见,同包中可见,子类不可见,其他包不可见. pr ...
- DevExpress控件开发常用要点(项目总结版)
使用DevExpress控件来做项目开发已经有很长一段时间了,在摸索开发到客户苛刻要求的过程中,其中碰到过很多问题需要解决的,随着一个个问题的解决,也留下很多对DevExpress控件的使用经验及教训 ...
- mvc url路由参数的加密和解密
查看某个信息的时候一般会在url上加上该信息在数据库中对应的主键id(而且一般是自增的) url是这样子的 xxxDetail/1 , 虽然对于我们开发人员来说可以这种显式的数据库主键会方便调试过程, ...
- 1.html5究竟是什么
1.html5的起源,历史背景…… 按照一般的套路,我这里应该对html5的起源和发展历史,其优越性等大书特书一番.但既然你有意识地专门去找类似的文章,说明你早有相应的认识,就算没有,类似的东西网上也 ...
- Python学习教程(learning Python)--2.2 Python下的变量基础
变量的基本概念,变量可以这样去理解,变量是一个值,这个值存储在计算机的内存里.以 网购为例,您在选购傻商品的时候,是在不同页面里选不同的商品,选好一件点击“放入购物车”,选完了再点击去结帐,这些商品的 ...
- webpack 学习笔记 01 使用webpack的原因
本系列文章实际上就是官网文档的翻译加上自己实践过程中的理解. 伴随着websites演化至web apps的过程,有三个现象是很明显的: 页面中有越来越多的Js. 客户端能做的事情越来越多. 越来越少 ...