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. mongodb分片集群(无副本集)搭建

    数据分片节点#192.168.114.26#mongo.cnfport=2001dbpath=/data/mongodb/datalogpath=/data/mongodb/log/mongodb.l ...

  2. 分享一个SqliteHelper类

    分享一个SqliteHelper类 SQLite作为一个本地文件数据库相当好用,小巧.快速.支持事务.关系型,甚至可以运行在Android上.在很久以前的一个项目中,我们用过它来将接收到的数据做本地统 ...

  3. Jenkins报错Caused: java.io.IOException: Cannot run program "sh" (in directory "D:\Jenkins\Jenkins_home\workspace\jmeter_test"): CreateProcess error=2, 系统找不到指定的文件。

    想在本地执行我的python文件,我本地搭建了一个Jenkins,使用了execute shell来运行我的脚本,发现报错 [jmeter_test] $ sh -xe D:\tomcat\apach ...

  4. jmeter中类型转换,字符串,转数字型或浮点型

    最近在做接口,使用的是jemter工具,在使用jemter工具中,基础的和高级的,在贺满的博客中可以查看到,https://www.cnblogs.com/puresoul/p/5092628.htm ...

  5. ASP.NET Core 程序发布到Linux(Centos7)爬坑实战

    前言 前阶段弄了个Linux系统想倒腾倒腾.NET Core,结果看了下网上的资料,大部分都是过期的,走了不少弯路,不过还好,今下午总算捣鼓出来了.Linux命令太多了,唉.血的教训:安装一定要看官网 ...

  6. Nginx 作为静态资源服务器

    Nginx Windows 版本的启动停止,重新加载配置 启动 Windows版本下载解压后有一个nginx.exe可执行文件,双击启动. 启动后 浏览器访问http://127.0.0.1 可以看到 ...

  7. oracle空间分析

    相交 sdo_relate(t.geom, sdo_geometry(:geometry,null),\'mask=ANYINTERACT\')=\'TRUE\'

  8. 手工检测SQL注入(安全性测试)

    手动你的ASP站可否注入: http://127.0.0.1/xx?id=11 and 1=1 (正常页面) http://127.0.0.1/xx?id=11 and 1=2 (出错页面) 检测表段 ...

  9. 2018年暑假ACM个人训练题6 解题报告

    A:水题 https://www.cnblogs.com/yinbiao/p/9311834.html B:考察进制的转化 https://www.cnblogs.com/yinbiao/p/9311 ...

  10. oracle静默安装

    RHEL6+oracle11.2 无界面化命令安装如下: 1.所需安装软件包检查: yum install binutils-2.* compat-libcap1* compat-libstdc++- ...