1. 原题链接

https://leetcode.com/problems/permutations/description/

2. 题目要求

给定一个整型数组nums,数组中的数字互不相同,返回该数组所有的排列组合

3. 解题思路

采用递归的方法,使用一个tempList用来暂存可能的排列。

4. 代码实现

 import java.util.ArrayList;
import java.util.List; public class Permutations46 {
public static void main(String[] args) {
int[] nums = {, , };
for (List l : permute(nums))
System.out.println(l.toString()); } public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
recursion(res,new ArrayList<>(),nums);
return res; } private static void recursion(List<List<Integer>> res, List<Integer> tempList,int[] nums){
if(tempList.size()==nums.length)
res.add(new ArrayList<>(tempList));
for(int i =;i<nums.length;i++){
if(tempList.contains(nums[i])) continue;
tempList.add(nums[i]);
recursion(res,tempList,nums);
tempList.remove(tempList.size()-);
}
}
}

LeetCode:46. Permutations(Medium)的更多相关文章

  1. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

  2. LeetCode:18. 4Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...

  3. LeetCode:15. 3Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...

  4. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  5. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  6. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  7. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  8. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

  9. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

随机推荐

  1. 【转】Windows Error Code(windows错误代码详解)

    本文来自: http://blog.sina.com.cn/s/blog_5e45d1be0100i0dr.html http://blog.sina.com.cn/s/blog_5e45d1be01 ...

  2. BZOJ1009:[HNOI2008]GT考试(AC自动机,矩乘DP)

    Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字. 他的不吉利数学A1A2...Am(0< ...

  3. Centos7 yum安装mysql

    参考此文档:http://www.jb51.net/article/116032.htm http://www.jb51.net/article/95399.htm 1.在官网下载mysql57-co ...

  4. Linux学习总结(一) windos环境vmware安装centos7

    一.在这里我先简单介绍下虚拟化技术[即在一台电脑上虚拟出子系统,而且可能需要多台服务器进行工作,一般都是linux系统做服务器或者学习研究之用], 二来我们可以有效利用一台pc的资源模拟出终端远程访问 ...

  5. Junit单元测试多线程的问题

    今天下午很快完成了一个接口的监控功能,然后屁颠屁颠地用Junit开始单元测试.然后我就开始陷入崩溃的边缘... 监控结束后需要将监控结果以邮件的形式发送给运营的小伙伴维护,前面测试还是很顺利,到了开多 ...

  6. java _this关键字的用法

    1:This关键字可以用于从一个构造方法调用另一个构造方法,可以用于避免重复代码 2:this的第二个用于this.xxx表示成员变量,成员变量的作用范围是  类   避免产生歧义 package c ...

  7. HDU 1017A Mathematical Curiosity (暴力统计特殊要求个数)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1017 A Mathematical Curiosity Time Limit: 2000/1000 M ...

  8. Linq 表达

    Lambda 简单了解 //Lambda //匿名方法 //delegate (Student s) { return s.Age > 12 && s.Age < 20; ...

  9. SpringSecurity

    1.1    SpringSecurity技术简介与使用 1.1.1     简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架. ...

  10. Python基础 List和Tuple类型

    python 创建list python 内置一种数据类型是列表: 列表是一种有序的集合,可以随时添加和 删除其中的元素,list 中的元素是按照顺序排列的.构建list 直接用 [ ], list ...