【题目】

Given a collection of distinct integers, return all possible permutations.

数组的组合情况。

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

【思路】

求子集,排列组合的数组题有固定模板。

【代码】

class Solution {
public List<List<Integer>> permute(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ans=new ArrayList<>();
List<Integer> tmp=new ArrayList<>();
fun(ans,tmp,nums);
return ans;
}
public void fun(List<List<Integer>> ans,List<Integer> tmp,int[] nums){
if(tmp.size()==nums.length){
ans.add(new ArrayList<>(tmp));
}
else{
for(int i=0;i<nums.length;i++){
if(tmp.contains(nums[i]))
continue;
tmp.add(nums[i]);
fun(ans,tmp,nums);
tmp.remove(tmp.size()-1);
}
}
}
}

[Leetcode 46]全排列 Permutations 递归的更多相关文章

  1. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  2. Java实现 LeetCode 46 全排列

    46. 全排列 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2] ...

  3. [leetcode] 46. 全排列(Java)

    46. 全排列 这题我们可以借用31. 下一个排列写的nextPermutation函数来做,稍微改造一下即可 注意要先给nums排个序 class Solution { // 当没有下一个排列时re ...

  4. leetcode 46. 全排列 及 47. 全排列 II

    46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...

  5. LeetCode 46. 全排列(Permutations)

    题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [ ...

  6. [LeetCode] 46. 全排列(回溯)

    ###题目 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], ...

  7. LeetCode 46 全排列

    题目: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3 ...

  8. LeetCode 46——全排列

    1. 题目 2. 解答 给定一个序列,序列中的任意一个数字都可以作为全排列的最后一位.然后,其余位置元素的确定便是剩余元素的一个全排列,也就是一个子问题. 例子中 [1, 2, 3] 的全排列,最后一 ...

  9. leetcode 46 全排列 (python)

    给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] ...

随机推荐

  1. sublime lincense for linux

    sublime lincense for linux Sublime Text 3.x (after Build 3133) —– BEGIN LICENSE —–TwitterInc200 User ...

  2. bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requested: lxml.

    python3 bs4解析网页时报错: bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requeste ...

  3. vue mand-mobile ui Stepper步进器默认值传字符串进去不起作用

    vue mand-mobile ui Stepper步进器默认值传字符串进去不起作用 Stepper 步进器 的默认值有没有弄过的,看了组件只能默认数字的,传字符串进去不起作用<div slot ...

  4. websocket 二合一

    server from flask import Flask, request, render_template from geventwebsocket.handler import WebSock ...

  5. vue计算属性和侦听器

    一.计算属性: main.js: var app = new Vue({ el: '#app', data: { math: 80, physics: 90, english: 30 }, compu ...

  6. Oarcle 入门之like关键字

    like关键字 模糊查询,有两个特殊的符号"%" ,"_" “%”表示匹配零个或若干字符 放哪边就表示哪边有零到若干个未知符号,其作用起到占位符的效果. “_” ...

  7. 笔记02 linux的一些命令sed

    #!/bin/bash # dataformat=`date +%Y-%m-%d-%H-%M` #进行文件件cp并重命名 nginx_home=/opt/modules/nginx-1.12/ cp ...

  8. 面向对象select方法

    <?php class Table{         protected $tablename;         protected $arrTable;        protected $w ...

  9. Netbeans and Remote Host for C/C++ Developing

    Netbeans and Remote Host for C/C++ Developing 很久以来,因为我不适应在 Linux 下使用 Vim, GCC, GDB 开发 C/C++ 程序,所以我一直 ...

  10. 03:CDN原理

    1.1 CDN简介 1.CDN作用(缓存静态资源) 1. CDN的全称Content Delivery Network,(缩写:CDN)即内容分发网络. 2. CDN解决由于网络带宽小.用户访问量大. ...