LeetCode:46. Permutations(Medium)
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)的更多相关文章
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- LeetCode:18. 4Sum(Medium)
1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...
- LeetCode:15. 3Sum(Medium)
1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
- LeetCode: 54. Spiral Matrix(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
随机推荐
- bzoj1434 [ZJOI2009]染色游戏
Description 一共n × m 个硬币,摆成n × m 的长方形.dongdong 和xixi 玩一个游戏, 每次可以选择一个连通块,并把其中的硬币全部翻转,但是需要满足存在一个 硬币属于这个 ...
- PHP时间戳和日期相互转换(转载)
在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strtotime()函数实现,下面我来给大家举例说明. 1.php中时间转换函数 strtotime ...
- .NET 中 如果一个Task A正在await另一个Task B,那么Task A是什么状态
新建一个.NET Core控制台程序,输入如下代码: using System; using System.Threading; using System.Threading.Tasks; class ...
- Impossible WHERE noticed after reading const tables
阿里云反馈的慢SQL,执行计划返回如下:Impossible WHERE noticed after reading const tables sql很简单: SELECT * FROM deposi ...
- RBG颜色对照表:有网址
RBG颜色对照表 大家都懂的RBG颜色对照表,想做一个有个性和美观的网页,风格是必须要有的,那么多姿多彩的颜色必然是装饰网页的一簇鲜花,为了方便查找比对颜色,就做了这个 网址为: http://too ...
- Dokcer-ce安装脚本
安装docker #!/bin/bash # coding: utf- # Copyright (c) set -e #返回值为0时,退出脚本 echo "1. 备份yum" { ...
- Spring 整合Mybatis dao原始方法
先看一下项目图,基本就理解了整合的内容 这次主角不再是Mybats的配置文件SqlMapConfig.xml了,而是Spring的applicationContext.xml applicationC ...
- phalcon框架与Volt 模块引擎 使用简介
———— 近期工作中web页面使用由C语言编写的Volt模板引擎,相比之前由js动态加载页面速度更快,更利于百度数据的抓取,现根据文档整理一下使用思路 (Volt是一个超快速和设计者友好的模板语言 ...
- 个人免签即时到账收款接口 bufpay.com 支持多账号收款
有很多 bufpay 的用户反馈,单个手机收款有些时候不太方便,切换手机太麻烦:或者是营业额比较多,希望分摊到多个账号上面. 基于以上的问题,bufpay 开发了多手机收款的功能:每个收款的手机安装 ...
- TCP/IP初识(一)
TCP/IP学习记录,如有错误请指正,谢谢!!! 什么是TCP/IP协议? TCP/IP协议族分为四层(另一个名字是Internet协议族(Internet Protocol Suite)):链路层. ...