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

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

递归法:例如,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的jsonp相关

    <!DOCTYPE html><html><head ><meta charset="utf-8"><script src=& ...

  2. StackExchange.Redis性能调优

    大家经常出现同步调用Redis超时的问题,但改成异步之后发现错误非常少了,但却可能通过前后记日志之类的发现Redis命令非常慢. PS: 以后代码都在Windows bash中运行,StackExch ...

  3. iOS微信打开App

    传统的方式是通过URL Scheme的方式,但是iOS9以后又出了新的更完美的方式Universal Links. 传统的URL Scheme方式微信内置的浏览器都是封锁了所有它投资的公司之外的,只有 ...

  4. Python SQLAlchemy基本操作和常用技巧

    转自:https://www.jb51.net/article/49789.htm 首先说下,由于最新的 0.8 版还是开发版本,因此我使用的是 0.79 版,API 也许会有些不同.因为我是搭配 M ...

  5. HttpSession 入门

    1. HttpSession 概述 位于 javax.servlet.http 包; HttpSession 是由 JavaWeb 提供的功能, 用来会话跟踪的类, session 是服务器端对象, ...

  6. CNI bridge 插件实现代码分析

    对于每个CNI 插件在执行函数cmdAdd之前的操作是完全一样的,即从环境变量和标准输入内读取配置.这在http://www.cnblogs.com/YaoDD/p/6410725.html这篇博文里 ...

  7. Scrapy框架-scrapy框架架构详解

    1.Scrapy框架介绍 写一个爬虫,需要做很多的事情.比如:发送网络请求.数据解析.数据存储.反反爬虫机制(更换ip代理.设置请求头等).异步请求等.这些工作如果每次都要自己从零开始写的话,比较浪费 ...

  8. (2.2)学习笔记之mysql基础操作(登录及账户权限设置)

    本系列学习笔记主要讲如下几个方面: 本文笔记[三:mysql登录][四:账户权限设置][五:mysql数据库安全配置] 三.mysql登录 常用登录方式如下: 四.账户权限设置 (4.1)查看用户表, ...

  9. An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.

    与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider: TCP ...

  10. sql server等待类型

    sql server 各种等待类型-转 sql server各个等待类型及解决办法:https://www.sqlskills.com/help/waits/writelog/其他等待类型的描述和解决 ...