给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
 
import java.util.ArrayList;
import java.util.List;
import java.util.Stack; public class FullSort { private List<List<Integer>> lists = new ArrayList<List<Integer>>();
public List<List<Integer>> permute(int[] nums) {
if(nums.length == 0) {
return lists;
}
else{
allSort(nums,new Stack<Integer>());
return lists;
}
}
//递归函数
public void allSort(int[]nums,Stack<Integer> stack)
{
//终止条件
if(stack.size() == nums.length )
{
lists.add(new ArrayList<Integer>(stack));
return;
}
for (int num : nums) {
if( stack.contains(num) ) {
continue;
}
stack.push(num);
allSort(nums,stack);
    //出栈
stack.pop();
}
}
}
 
 

LeetCode——全排列的更多相关文章

  1. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  2. [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...

  3. Leetcode 全排列专题(更新ing)

    总览 涉及到的题目有 题号 名字 难度 Leetcode 60 第k个排列 中等 Leetcode 46 全排列 中等 待更新...... Leetcode 46 全排列 题目 基础题 给定一个 没有 ...

  4. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  5. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  6. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  8. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  9. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

随机推荐

  1. Nginx服务应用

    虚拟主机 基于域名的虚拟主机 所谓基于域名的虚拟主机,意思就是通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站都是使用基于域名的虚拟主机 基 ...

  2. mongdb aggregate聚合操作

    1.数据准备 查看前一篇group操作 2.aggregate函数参数讲解 mysql mongdb===================WHERE --->$match GROUP BY -- ...

  3. [Hibernate]知识点

    本笔记只介绍注解的方法 一.准备工作: a.添加三个pojo类: Product: package pojo; import javax.persistence.*; import java.util ...

  4. Session中的方法概述

    一.操作实体对象 delete()把持久化或游离转为删除状态 save()把临时状态变为持久化状态(交给Sessioin管理) saveOrUpdate()把临时或游离状态转为持久化状态 update ...

  5. 监控系统-PMM

    Percona Monitoring and Management (PMM)是一款开源的用于管理和监控MySQL和MongoDB性能的开源平台 通过PMM客户端收集到的DB监控数据用第三方软件Gra ...

  6. vue中封装swipe组件

    <template> <!-- TODO swipe --> <div id="hy-swiper"> <div class=" ...

  7. Kali Linux 2019.2安装谷歌输入法

    前言 Linux下常用的中文输入法平台有IBus.fcitx和scim.scim现在维护滞后,不推荐使用. IBus ("Intelligent Input Bus") 是一个 输 ...

  8. 【Python开发】python读写文件,和设置文件的字符编码比如utf-8

    一. python打开文件代码如下: f = open("d:\test.txt", "w") 说明: 第一个参数是文件名称,包括路径: 第二个参数是打开的模式 ...

  9. JS延迟加载的几种方式

    参考链接:https://blog.csdn.net/meijory/article/details/76389762

  10. 【STM32】STM32串口配置的一般步骤(库函数)

    STM32串口配置的一般步骤(库函数)(1)串口时钟使能:RCC_APBxPeriphClockCmd();    GPIO时钟使能:RCC_AHBxPeriphClockCmd();(2)引脚复用映 ...