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

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

递归法:例如,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. orchestrator-Raft集群部署

    本文简要说明下orchestrator的Raft集群部署,其实部署很简单主要是好好研究下配置文件的配置,这里我的样例配置文件暂时只适用于我们这块业务 如果您自己使用请根据情况自行修改. 主要通过配置文 ...

  2. 『浅入浅出』MySQL 和 InnoDB

    作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系 ...

  3. 数据库操作(使用FMDB)

    iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...

  4. JD-GUI

    JD-GUI http://jd.benow.ca/ JD-GUI可到官網直接下載.官網除了JD-GUI之外,另提供了Eclipse(JD-Eclipse)和IntelliJ(JD-IntelliJ) ...

  5. SpringMVC 之 RESTful 风格的增删改查

    1. 视图和视图解析器 视图解析器 请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View 或 ModelMap 等类型的处理方法, SpringMVC 也会 ...

  6. 解决ajax post json数据 后端无法收到的问题

    如图,想把input框中的文字以json格式post出去,结果后端收不到 使用wireshark抓包,根本没有抓到发往服务器的包,说明错误在前端. 后来发现ajax post json数据的时候key ...

  7. python return中的or和and语句

    python return中的or和and语句 1.二元运算: 如果一个True,一个False或两个false: return True and False # 返回False return Tru ...

  8. git "Could not read from remote repository.Please make&n

    git "Could not read from remote repository.Please make sure you have the correct access rights. ...

  9. 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题

    1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...

  10. LRU经典算法的原理与实现

    LRU  least recently used.顾名思义,是根据数据的活跃度进行更新的缓存算法. LRU Cache的LinkedHashMap实现: LinkedHashMap自身已经实现了顺序存 ...