Problem:给出一个数组(数组中的元素均不相同),求出这个数组能够产生的所有全排列
采用递归算法,传入参数
List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used
 
  其中list保存最终结果
  tempList保存其中一个全排列
  nums为最初的数组
  used随着计算不断更新,保存数组中元素是否已经使用过
参考代码:
 
package leetcode_50;

import java.util.ArrayList;
import java.util.List; /***
*
* @author pengfei_zheng
* 给定数组元素均不相同,求出所有全排列
*/
public class Solution46 {
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
prem(list,new ArrayList<>(),nums,new boolean[nums.length]);
return list;
} private static void prem(List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used) {
if(tempList.size()==nums.length){
list.add(new ArrayList<>(tempList));
}
else{
for(int i = 0; i < nums.length; i++){
if(used[i] || i>0 && nums[i]==nums[i-1] && !used[i-1]) continue;
used[i]=true;
tempList.add(nums[i]);
prem(list,tempList,nums,used);
used[i]=false;
tempList.remove(tempList.size()-1);
}
}
}
public static void main(String[]args){
int []nums={1,2,3};
List<List<Integer>> list = permute(nums);
for(List<Integer> item:list){
System.out.println(item);
}
}
}

LeetCode 46 Permutations(全排列问题)的更多相关文章

  1. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  2. [leetcode]46. Permutations全排列(给定序列无重复元素)

    Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...

  3. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  4. 【LeetCode】Permutations(全排列)

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

  5. [leetcode]47. Permutations全排列(给定序列有重复元素)

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

  6. 46. Permutations (全排列)

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

  7. 46 Permutations(全排列Medium)

    题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...

  8. LeetCode 046 Permutations 全排列

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

  9. [LeetCode] 47. Permutations II 全排列 II

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

随机推荐

  1. php 正则获取字符串中的汉字preg_match_all

    preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $list[$i]['iparr'], $matches); $list[$i]['iparr'] = join(' ...

  2. VVDocumenter规范注释生成器

    下载地址:https://github.com/onevcat/VVDocumenter-Xcode PS:Xcode 8.0 默认支持了,但是是关闭状态,需要在终端输入如下命令开启,然后重启 Xco ...

  3. 【NLP】Stanford

    http://web.stanford.edu/class/cs224n/syllabus.html https://www.youtube.com/watch?v=OQQ-W_63UgQ&l ...

  4. Java学习之——泛型

    1.概要 generics enable types (classes and interfaces) to be parameters when defining classes, interfac ...

  5. 在Unity中查找缺失的引用

    这篇博客是查找unity中缺失引用的一个简单简短的解决方案.你可以从GitHub上获取源码. 缺失引用 一个丢失引用与没有引用(在检视表显示“None”)是完全不同的概念.这些友各种原因造成,比如:把 ...

  6. Redis集群版在Java中的应用

    1.配置redis集群 <?xml version="1.0" encoding="UTF-8"?> <redisCluster> &l ...

  7. 对SQL语句进行过滤的函数

    /// <summary> /// 过滤SQL非法字符串 /// </summary> /// <param name="value">< ...

  8. Android 4.0以上BlurMaskFilter效果无效

    Android MaskFilter的基本使用: MaskFilter类可以为Paint分配边缘效果.        对MaskFilter的扩展可以对一个Paint边缘的alpha通道应用转换.An ...

  9. Linux+Redis实战教程_day03_4、通用redis命令【重点】

    4.通用redis命令[重点] Redis五种数据类型,String,hash,list,set,有序set l keys pattern:获取所有与pattern匹配的key,返回所有与该key匹配 ...

  10. MongoDB文档的增删改操作

    上一篇文章中介绍了MongoDB的一些基本知识,同时看到了怎么启动一个MongoDB服务,并且通过MongoDB自带的shell工具连接到了服务器. 这一次,就通过MongoDB shell介绍一下对 ...