描述:给定一个数字列表,返回其所有可能的排列。

样例:给出一个列表[1,2,3],其全排列为:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

说明:分别使用递归和非递归实现

Java

1、递归

 public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null)
return res;
if(nums.length == 0)
{
res.add(new ArrayList<Integer>());
return res;
} ArrayList<Integer> list = new ArrayList<>();
dfs(res, list, nums);
return res;
} private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums) {//深度优先 int n = nums.length;
if(list.size() == n)
{
res.add(new ArrayList<Integer>(list));
return;
} for(int i = 0;i < n;i++) {
if(list.contains(nums[i]))
continue;
list.add(nums[i]);
dfs(res, list, nums);
list.remove(list.size() - 1);
}
}
}

2、非递归

 public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(nums == null)
return res;
if( nums.length == 0){
res.add(new ArrayList<Integer>());
return res;
}
List<Integer> list = new ArrayList<>();
list.add(nums[0]);
res.add(new ArrayList<Integer>(list)); for(int i=1;i<nums.length;i++){
int size1 = res.size();
for(int j=0;j<size1;j++){
int size2 = res.get(0).size();
for(int k=0;k<=size2;k++){
ArrayList<Integer> temp = new ArrayList<>(res.get(0));
temp.add(k,nums[i]);
res.add(temp);
}
res.remove(0);
}
}
return res;
}
}

LintCode——全排列的更多相关文章

  1. [LintCode] 全排列

    递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of permutati ...

  2. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  3. lintcode 中等题:permutations II 重复数据的全排列

    题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...

  4. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  5. lintcode Permutation Index

    题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  8. LintCode 190: Next Permutation

    LintCode 190: Next Permutation 题目描述 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列. 如果没有下一个排列,则输出字典序最小的序列. 样例 ...

  9. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

随机推荐

  1. 【转】Nginx学习---深入浅出Nginx的介绍

    [原文]https://www.toutiao.com/i6595428119933354500/ Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在 ...

  2. mysql数据库管理工具(navicat for mysql) 10.1.7 绿色中文版

    Navicat for MySQL:Navicat for MySQL 是一套专为 MySQL 设计的高性能数据库管理及开发工具.它可以用于任何版本 3.21 或以上的 MySQL 数据库服务器,并支 ...

  3. [NOIP2016 DAY1 T2]天天爱跑步-[差分+线段树合并][解题报告]

    [NOIP2016 DAY1 T2]天天爱跑步 题面: B[NOIP2016 DAY1]天天爱跑步 时间限制 : - MS 空间限制 : 565536 KB 评测说明 : 2s Description ...

  4. Python os.md

    os 便携式访问操作系统的特定功能.os模块提供了对特定平台模块(如posix, nt, mac)的封装, 函数提供的api在很多平台上都可以相同使用, 所以使用os模块会变得很方便. 但不是所有函数 ...

  5. mysql 压缩备份 压缩还原 命令

    .mysqldump 备份并压缩sql文件 mysql>mysqldump -h主机ip -u用户名 -p密码(也可不输入) 数据库名 | gzip > 压缩后文件位置 .mysql直接用 ...

  6. leetcode16—3 Sum Closet

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  7. torchvision

    torchvision是一个包,它服务于pytorch深度学习框架,用来生成图片,视频数据集,模型类和预训练的模型torchvision由以下四个部分组成:1. torchvision.dataset ...

  8. JAVA框架:hibernate(二)

    一.事务操作. 代码: @Test public void tr_Up(){ Session session=hibernateUtils.getSession(); Transaction tran ...

  9. P3195 [HNOI2008]玩具装箱TOY

    列出DP方程式:设f[i]表示分组完前i件物品的最小花费,为方便计算,设sum[i]表示是前i件物品的长度和. f[i]=min(f[j]+(sum[i]-sum[j]+i-j-L-1)^2) [0& ...

  10. OO课程学期末总结

    OO课程学期末总结 测试VS正确性论证 OCL vs JSF 对象约束语言(Object Constraint Language), 简称OCL, 是一种指示用户建模系统中的限制方式. 他是UML可选 ...