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

样例:给出一个列表[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. 【排序算法】冒泡排序(Bubble Sort)

    0. 说明 参考 维基百科中的冒泡排序 冒泡排序 (Bubble Sort) 是与插入排序拥有相等的执行时间,但是两种算法在需要的交换次数却很大地不同. 在最坏的情况,冒泡排序需要 O(n2)  次交 ...

  2. November 09th, 2017 Week 45th Thursday

    If we did all the things we are capable of, we would literally astound ourselves. 我们如果尽全力去完成我们能做到的事情 ...

  3. windows系统显示隐藏文件以及显示文件扩展名

    1.XP系统 打开“我的电脑”,“工具”,“文件夹选项” 勾选如下图 2.win7系统 打开“计算机”,“组织”,“文件夹和搜索选项” 勾选如下图 3.win10系统 打开“此电脑”,“查看”,勾选如 ...

  4. c++向量

    https://blog.csdn.net/suxiao_shaoer/article/details/52180087 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个 ...

  5. 面向对象程序设计__Task6_Calculator1.6.2

    The 4th part of the Calculator program _ Interface 题目链接:第六次作业(计算器第四步) github链接:Calculator_1.6.2 第六次作 ...

  6. css图片替换方法

    图片替换主要是指将文字替换成图片的技术,即在html语句中使用文字,浏览器显示时用对应的图片显示.其意义在于便于做网站优化(SEO),因为文字才是搜索引擎寻找的主要对象. https://www.cn ...

  7. IE8 input X 去掉文本框的叉叉和密码输入框的眼睛图标

    从IE 10开始,type="text" 的 input 在用户输入内容后,会自动产生一个小叉叉(X),方便用户点击清除已经输入的文本 对于type="password& ...

  8. animate is not a function(zepto 使用报错)[转]

    animate is not a function(zepto 使用报错) 1.为什么使用zepto写animate报错? 因为zepto默认构建包含: Core, Ajax, Event, Form ...

  9. Redis系列二:reids介绍

    一.什么是redis.redis有哪些特性.redis有哪些应用场景.redis的版本 1. 什么是redis redis是一种基于键值对(key-value)数据库,其中value可以为string ...

  10. 21天,搞定软件测试从业者必备的Linux命令

    开始之前,先同步一个结论: 对于软件测试从业者,如果你至今为止,还不懂Linux,或者完全没有接触Linux ,这是一件很危险和恐怖的事 . 此刻.现在.果断,学习Linux命令 . 如果你工作中,完 ...