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. PhoneGap 的存储 API_localStorage 和 sessionStorage

    一.介绍 1.为了替代Cookile这门古老的客户端存储技术,Html5的WEB Storage Api 提供了俩中在 客户端存储数据库的方法:localStorage 和 sessionStorag ...

  2. 4.30-5.1cf补题

    //yy:拒绝转载!!! 悄悄告诉你,做题累了,去打两把斗地主就能恢复了喔~~~ //yy:可是我不会斗地主吖("'▽'") ~~~那就听两遍小苹果嘛~~~ 五一假期除了花时间建模 ...

  3. poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】

    题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...

  4. EOJ Monthly 2019.2 (based on February Selection) D 进制转换 【数学 进制转换】

    任意门:https://acm.ecnu.edu.cn/contest/140/problem/D/ D. 进制转换 单测试点时限: 2.0 秒 内存限制: 256 MB “他觉得一个人奋斗更轻松自在 ...

  5. vue.js 组件监听

    一.在通过点击事件触发的子组件中: addCart(event) { if (!event._constructed) { return; } if (!this.food.count) { Vue. ...

  6. 使用QT开发GoogleMap瓦片显示和下载工具

    之前做项目的时候经常遇到需要大量地图背景数据,然后没有数据被逼着去Google上下载瓦片数据在拼接成整张影像的工作,其实遥感影像晚上有很多可以下载到的,但是大部分是作为研究用的,作为GIS的背景图或者 ...

  7. 【题解】洛谷P2023 [AHOI2009] 维护序列(线段树)

    洛谷P2023:https://www.luogu.org/problemnew/show/P2023 思路 需要2个Lazy-Tag 一个表示加的 一个表示乘的 需要先计算乘法 再计算加法 来自你谷 ...

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

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

  9. jquery实现顶部浮动效果

    示例: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <tit ...

  10. AltiumDesigner16笔记

    集成库(*.intlib)没有办法直接修改库文件,但是*.schlib可以编辑的. 原理图元件批量修改可以从Tools-> Update from libraries进行. PCB封装批量修改: ...