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. HTTP 中状态码 302的使用场景

    一直都知道302是临时重定向,可是不懂为什么要用这个,直到看到了这个 这样就可以用一个URL,来访问其他的URL上的资源了,非常的nice

  2. 针对Restful风格参数传递的请求获取真实url

    昨天遇到这样一个问题,先简单介绍下. 业务场景 我们想要统计热点请求URL,进而进行分析优化 方案 通过过滤器获取到请求url(调用方法request.getservletpath),通过redis进 ...

  3. Sublime Text 3中关闭记住上次打开的文件

    使用UltraEdit的时候,每次安装后就得修改一堆配置,其中一项便是关闭“打开上一次未关闭的文件”,Sublime Text 2也有这么一个默认的功能,在实际使用中,这种方式确实可以较快速的访问文件 ...

  4. fc全连接层的作用、卷积层的作用、pooling层、激活函数的作用

    fc:1.起到分类器的作用.对前层的特征进行一个加权和,(卷积层是将数据输入映射到隐层特征空间)将特征空间通过线性变换映射到样本标记空间(也就是label) 2.1*1卷积等价于fc:跟原featur ...

  5. Coursera机器学习基石 第2讲:感知器

    第一讲中我们学习了一个机器学习系统的完整框架,包含以下3部分:训练集.假设集.学习算法 一个机器学习系统的工作原理是:学习算法根据训练集,从假设集合H中选择一个最好的假设g,使得g与目标函数f尽可能低 ...

  6. html拼接时onclick事件传递json对象

    Bootstrap Table 中拼装onclick传递json对象会造成[object,object]错误,反正各种传值不成功, 应该是因为json对象中的‘’‘’引号冲突吧,直接把json对象转成 ...

  7. 设置UI控件的Layer属性(边框可见,边框颜色,边框宽度,边框圆角)

    设置UI控件的Layer属性 #import "ViewController.h" @interface ViewController () @property (strong, ...

  8. EF Core如何输出日志到Visual Studio的输出窗口

    我们在使用EF Core的时候,很多时候需要在Visual Studio的输出窗口中知道EF Core在后台生成的SQL语句是什么,这个需求可以通过自定义EF Core的ILoggerFactory和 ...

  9. 手机端app开发初识

    1.所需软件说明 百度云下载链接: https://pan.baidu.com/s/1-TEQZP9QbJSlGSYedyAUFw密码: 2z8l 或者官方链接: Hbuilder:http://ww ...

  10. 【js】数组添加与删除

    做个表格,就会容易记忆四种方法.    返回值  是否改变数组长度 位置 功能 push() 改变数组的长度   是    末位  添加 unshift() 改变数组的长度  是 首位 添加 pop( ...