【LeetCode练习题】Permutations
全排列
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]
.
题目意思:
给定一个集合,求全排列。
解题思路:
一般的话,有两种思路可以考虑。递归和DFS遍历树。
这里我只用了递归的方法,关于怎么建一棵树然后DFS,可以参考 这里。
这对于练习对递归的理解真是个不错的题目。
以[1,2,3]举例,可以用纸笔画一画,分别以1,2,3为根,画出三棵树来,然后由根到每一个节点就对应着一个排列。
首先最外层肯定要有一个for循环来一次遍历这里面的元素决定出根节点,然后在循环中递归。
其中,有两点需要注意的:
- 要设置一个bool型的visited数组,大小和num一样大,来标记num中对应的元素是否已经访问过了,对于还没有访问过的,我们才可以把它加到element里来。这样可以避免在树的不同层出现同一个数。
- 在element已经完成了一次排列后,对element进行pop_back(),然后将pop出来的数对应的visited设置为true,表示它现在可以被访问了。举例来说,当element中已经有了[1,2,3]并且添加到result里后,进行一个pop_back()剩下[1,2]并将visited[2]置为true,然后跳到上一层调用,再pop_back()剩下[1]并将visited[1]置为true,此时for循环i++,此时visited[2]为true,即向element里添加num[2],element变成[1,3],接着再调用递归,在下一层因为visited[0]为false,visited[1]为true,element将添加num[1]变成[1,3,2]。
对递归不熟的朋友,可以一边对着代码,一边用笔画一下几个树,过几遍就OK拉!
代码如下:
#include <vector>
#include <iostream>
using namespace std; class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> result;
vector<int> element; int len = num.size();
bool *visited = new bool[len];
memset(visited,false,len); helper(num,result,element,visited);
return result;
} private:
void helper(vector<int> &num,vector<vector<int>> &result,vector<int> &element,bool *visited){
if(element.size() == num.size()){
//element中元素和num中元素一样多,说明得到了一个全排列,放进result里
result.push_back(element);
return ;
} for(int i = ; i < num.size(); i++){
if(!visited[i]){
visited[i] = true;
element.push_back(num[i]);
helper(num,result,element,visited); element.pop_back();
visited[i] = false;
}
}
}
}; int main(){
Solution s;
vector<int> num;
num.push_back();
num.push_back();
num.push_back();
s.permute(num);
}
【LeetCode练习题】Permutations的更多相关文章
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
随机推荐
- PKU 1050-To The Max(找矩形内元素最大和)
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- LeeCode-Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- Linux NFS服务器的安装与配置(转载)
一.NFS服务简介 NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操 ...
- SQL Server 中使用参数化Top语句
在T-Sql中,一般top数据不确定的情况下,都是拼sql,这样无论是效率还是可读性都不好.应该使用下面参数化Top方式:declare @TopCount int set @TopCount = 1 ...
- Python 自动化脚本学习(三)
函数 例子 def hello(): print("hello" + "world"); 有参数的函数 def hello(name): print(" ...
- execute immediate的简单用法(oracle)
直接上示例代码: create or replace procedure proc_test( --参数区域 ) is --变量区域 --sql脚本 v_sql ) :=''; --记录学生数量 v_ ...
- 只允许指定IP访问指定端口 ufw
那天工作需要在服务器上指定ip才可以访问指定端口,配置命令如下: (服务器是ubuntu 14.04系统) apt-get install ufw ufw enable ufw default den ...
- mysql binlog参数设置
1.mysql有许多系统变量,可以设置,系统变量设置不同,不同的系统将导致执行状态. 故mysql提供两组命令,分别查看系统设置和执行状态. 1.系统设置: SHOW [GLOBAL | SESSIO ...
- Qt开始学习的一些问题
1.需要将qmake.moc和qvfb的路径加入到系统的环境变量: qmake.moc:export PATH=$PATH:/usr/local/Trolltech/QtEmbedded-4.6.1- ...
- apache 三种工作模式的讲解
Apache 2.X 支持插入式并行处理模块,称为多路处理模块(MPM).在编译apache时必须选择也只能选择一个MPM,对类UNIX系统,有几个不同的MPM可供选择,它们会影响到apache的速 ...