问题描述:给定一个数组,数字中数字不重复,求所有全排列。

算法分析:可以用交换递归法,也可以用插入法。

递归法:例如,123,先把1和1交换,然后递归全排列2和3,然后再把1和1换回来。1和2交换,全排列1和3,再把1和2交换回来。1和3交换,全排列2和1,再把1和3交换回来。

//递归方法
public List<List<Integer>> permute2(int[] num) {
List<List<Integer>> result = new ArrayList<>();
permute(num, 0, result);
return result;
} void permute(int[] num, int start, List<List<Integer>> result) { if (start == num.length)
{
ArrayList<Integer> item = convertArrayToList(num);
//List<int[]> list = Arrays.asList(num);这个方法适合String,而不适合int,会把int[]当成一个元素加入list
result.add(item);
}
for (int j = start; j < num.length; j++)
{
//递归方法,首先1和1交换,求除nums[1]外的序列的全排列,然后1和1再交换回来。1和2交换,求除了nums[1]之外的序列的全排列,然后1和2再交换回来。
swap(num, start, j);
permute(num, start + 1, result);
swap(num, start, j);
}
} private ArrayList<Integer> convertArrayToList(int[] num) {
ArrayList<Integer> item = new ArrayList<Integer>();
for (int h = 0; h < num.length; h++) {
item.add(num[h]);
}
return item;
} private void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

插入法:例如123,1开始有1个插入位置得到序列[1],然后2有两个插入位置,得到序列[2,1],[1,2],然后3有三个插入位置,得到[3,2,1],[2,3,1],[2,1,3],[3,1,2],[1,3,2],[1,2,3]

public List<List<Integer>> permute(int[] num) {
List<List<Integer>> result = new ArrayList<>();
//start from an empty list
result.add(new ArrayList<Integer>()); for (int i = 0; i < num.length; i++)
{
List<List<Integer>> current = new ArrayList<>(); for (List<Integer> l : result)
{
//插入size+1个位置
for (int j = 0; j < l.size()+1; j++)
{
l.add(j, num[i]);
ArrayList<Integer> temp = new ArrayList<Integer>(l);
current.add(temp);
l.remove(j);
}
} result = new ArrayList<>(current);
} return result;
}

Permutations,全排列的更多相关文章

  1. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  2. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  3. lintcode 中等题:permutations 全排列

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

  4. [leetcode]46. Permutations全排列(给定序列无重复元素)

    Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...

  5. [leetcode]47. Permutations全排列(给定序列有重复元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. 46. Permutations (全排列)

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

  7. 046 Permutations 全排列

    给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3,2 ...

  8. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  9. 46 Permutations(全排列Medium)

    题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...

  10. permutations(全排列)

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

随机推荐

  1. 记录--jquery 获取父级、子级、兄弟元素 + 实例

    需求如下: 三条数据,需点击其中一条数据在其下面展示与此数据关联的图片.主要功能可能是在点击的数据下增加显示行 思路: 把需要点击增加的数据先隐藏.点击后再将其显示出来. 知识点: jQuery.pa ...

  2. MVC4 WebAPI中如何返回一张图片

    public HttpResponseMessage Get(string imageName, int width, int height) { Image img = GetImage(image ...

  3. 推荐10 个短小却超实用的 JavaScript 代码段

    1. 判断日期是否有效 JavaScript中自带的日期函数还是太过简单,很难满足真实项目中对不同日期格式进行解析和判断的需要.jQuery也有一些第三方库来使日期相关的处理变得简单,但有时你可能只需 ...

  4. AwesomePerfCpp 性能优化

    Contents Talks Articles Sites/Blogs Tools Libraries Books About Talks 2013: Going Native 2013 - Andr ...

  5. elastic search 查询语句

    部署了半个月,分析一下数据: 需要提前知道的是,tpot中,每天的数据存一个index,然后每个index里面有不同的type,每条请求一个document 共24万条请求: 查看整个集群所有数据 以 ...

  6. vue-cli注册全局组件

    在main.js开头引入组件,然后注册组件,例如: import Vue from 'vue' import VueRouter from 'vue-router' import VueResourc ...

  7. [NOIP2018TG]保卫王国

    [NOIP2018TG]保卫王国 BZOJ luogu 当动态dp模板题写的,(全集-最大点权独立集)不能放军队的+inf,必须放军队-inf即可 注意矩阵乘法的顺序问题 #define ll lon ...

  8. Spring-Spring概述

    Spring概述 Spring是最受欢迎的企业级Java应用程序开发框架.数以百万的来自世界各地的开发人员使用Spring框架来创建好性能.易于测试.可重用的代码. Spring框架是一个开源的Jav ...

  9. 【JUnit】junit4的几个assert方法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhtao01/article/details/27858225 在静态类junit.framewor ...

  10. 维多利亚的秘密 golang入坑系列

    原文在gitbook,字字原创,版权没有,转载随意. 在写本文的前一天,2017维密在上海开始了. 为了纪念屌丝界的盛世,特为本节起名维多利亚的秘密.现在的社会,要想出名只有抓眼球.所以写份技术文章, ...