说明

算法: 排列permutations
地址: https://leetcode.com/problems/permutations/
Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]
]

遍历解法实现实现

逻辑分解:
遍历数组中的每个元素,初始化为空结果数组[[]]

  1. 如果结果数组为空,添加到数组列表中[[1]]
  2. 如果结果数组有1个数据,则插入到已有数组的任意一个位置,比如2,那么结果就有2在位置0,和位置1,[[2,1],[1,2]];
  3. 如果结果数组有多个数据,则挨个取出list,并把新元素接入到已有数组的任意一个位置:
  1. 先取出[2,1], 那么新元素3,插入在位置0,位置1,位置2的可能,结果是[3, 2, 1], [2, 3, 1], [2, 1, 3]
  2. 后取出[1, 2], 那么新元素3,插入在位置0,位置1,位置2的可能,结果是[3, 1, 2], [1, 3, 2], [1, 2, 3].
public List<List<Integer>> permuteWithIterate(int[] nums) {LinkedList<List<Integer>> result = new LinkedList<>();result.add(new ArrayList<Integer>());int size;for (int n: nums) {size = result.size();for (; size > 0; size--) {List<Integer> resultItem = result.pollFirst();for (int i = 0; i <= resultItem.size(); i++) {List<Integer> newList = new ArrayList<>(resultItem);newList.add(i, n);result.add(newList);}}}return result;}

代码输出逻辑讲解

Code flownums = 1,2,3start = 0, permutation = []
i = 0, newPermutation = [1]start = 1, permutation = [1]i = 0, newPermutation = [2, 1]start = 2, permutation = [2, 1]i = 0, newPermutation = [3, 2, 1]i = 1, newPermutation = [2, 3, 1]i = 2, newPermutation = [2, 1, 3]i = 1, newPermutation = [1, 2]start = 2, permutation = [1, 2]i = 0, newPermutation = [3, 1, 2]i = 1, newPermutation = [1, 3, 2]i = 2, newPermutation = [1, 2, 3]

递归解法实现

public List<List<Integer>> permute(int[] nums) {List<List<Integer>> result = new ArrayList<List<Integer>>();if (nums == null || nums.length == 0) {return result;}// backtrackingaddItemInAllPosition(nums, 0, new ArrayList<Integer>(), result);return result;}private void addItemInAllPosition(int[] nums, int start, List<Integer> list, List<List<Integer>> result) {if (list.size() == nums.length) {result.add(list);return;}for (int i = 0; i <= list.size(); i++) {List<Integer> newList = new ArrayList<>(list);newList.add(i, nums[start]);addItemInAllPosition(nums, start + 1, newList, result);}}

挨个遍历,选择或者放弃

思路:

  1. 遍历所有的元素,要么选择,要么不选择(list.remove(list.size() - 1))。
  2. 判断元素是否已经存在,存在则跳过。
  3. 列表元素个数与数组元素个数相同,则加入结果列表。
public List<List<Integer>> permuteWithPickupOrNo(int[] nums) {List<List<Integer>> resultList = new ArrayList<List<Integer>>();if (nums == null || nums.length == 0) {return resultList;}recursive(nums, new ArrayList<Integer>(), resultList);return resultList;}public void recursive(int[] nums, List<Integer> list, List<List<Integer>> resultList) {if (list.size() == nums.length) {resultList.add(new ArrayList<Integer>(list));return;}for(int item: nums) {if (list.contains(item)) {continue;}list.add(item);recursive(nums, list, resultList);list.remove(list.size() - 1);}}

代码下载

https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/backtracking/Permutations.java

算法:回溯五 数组全排列permutations相关推荐

  1. 回溯java算法_聊聊算法——回溯算法

    "递归只应天上有,迭代还须在人间",从这句话我们可以看出递归的精妙,确实厉害,递归是将问题规模逐渐减小, 然后再反推回去,但本质上是从最小的规模开始,直到目标值,思想就是数学归纳法 ...

  2. 基本算法-回溯法(迷宫问题)

    作者:翟天保Steven 版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处 前言 本文介绍一种经典算法--回溯法,可作为迷宫问题的一种解法,以下是本篇文章正文内容,包括算法 ...

  3. KMP算法之next数组详解

    KMP算法之next数组详解 KMP算法实现原理 KMP算法是一种非常高效的字符串匹配算法,下面我们来讲解一下KMP算如何高效的实现字符串匹配.我们假设如下主串和模式串: int i;//i表示主串的 ...

  4. 数据结构与算法之KMP算法中Next数组代码原理分析

    2019独角兽企业重金招聘Python工程师标准>>> 一.KMP算法之Next数组代码原理分析       1.Next数组定义 当模式匹配串T失配的时候,Next数组对应的元素指 ...

  5. 桂 林 理 工 大 学实 验 报 告实验五 数组

    桂 林 理 工 大 学 实  验  报  告 班级 计算机类20 学号32020520 姓名  实验名称实验五 数组 日期 2020年 11月28日 一.实验目的: 熟悉掌握一维数组和二维数组的定义和 ...

  6. 漫话算法[回溯]:从《大话西游》到掌握回溯思想!

    快来和叮当学算法吧! From All CV to No CV!我的梦想是编程不再CV,算法不再死记硬背! 漫话算法[看电影学回溯算法]从<大话西游>到掌握回溯算法 回溯算法简介 回溯算法 ...

  7. (算法)求数组中数字组合(可多值组合)相加最接近目标数的组合(可能多个)

      今天没事,撸一道算法题   题目要求: 给出一个升序排序的可能重复值的数字数组和一个目标值其中目标值大于数组中最小数,求数组中数字组合(可多值组合)相加最接近目标数的组合(可能多个)不考虑空间复杂 ...

  8. 算法报告五--跳马问题

    算法报告五 跳马问题                                                                16122020   钟顺源 一.题目大意 给定8* ...

  9. 现代优化算法(五): 蚁群算法

    组合优化算法系列: 现代优化算法 (一):模拟退火算法 及应用举例 现代优化算法 (二): 遗传算法 及应用举例 现代优化算法(三):禁忌搜索算法 现代优化算法(四):改进的遗传算法 现代优化算法(五 ...

  10. 算法:五笔编码,如何根据输入的词条自动生成输入编码

    算法:五笔编码,如何根据输入的词条自动生成输入编码 一.想要实现的 最近做的一个五笔码表工具,想要实现根据用户输入的词条自动生成输入编码. 比如: 输入 我们 生成 trwu 输入 五笔基础知识 生成 ...

最新文章

  1. pandas使用groupby函数和cumsum函数计算每个分组内的数值累加值、并生成新的dataframe数据列( cumulative sum of each group in dataframe
  2. 基于matlab的信号去噪,基于matlab的信号去噪研究.doc
  3. 阿里云centos7通过yum安装 Mysql 8.0.11
  4. 【PC工具】chrome浏览器插件vimium:传说上网可以不用鼠标。VIM入门工具,妈妈再也不用担心我学不会vim了...
  5. Gradle常用配置
  6. python字符串类型_python字符串类型介绍
  7. 云炬随笔20190701
  8. 服务器实际显示内存,服务器实际显示内存大小
  9. 利用官方的vue-cli脚手架来搭建Vue集成开发环境
  10. 《数据安全警示录》一书修订版出版
  11. 生命游戏c语言代码,c++生命游戏源码
  12. php框架开发(草稿)
  13. NoSQL数据库应用
  14. latex模板——计算机学报
  15. er ubnt x设置教程_ubnt的er-x做交换机应该怎么设置啊?
  16. 图片去水印的原理_图片去水印方法 图片如何去掉水印
  17. 活着的传奇——《DOOM启世录》摘录
  18. Python库——Faker
  19. HashMap是怎样实现快速查找的
  20. 函数指针的用法以及用途详解

热门文章

  1. python unrar问题_Python提取/unrar RAR文件错误
  2. [转]nodejs Error: request entity too large解决方案
  3. [译] 实例解析 ES6 Proxy 使用场景
  4. Android CircleMenu:旋转转盘选择Menu
  5. ruby array 额
  6. Daily Scrum 10.23
  7. C# 简单封装一个XML文件读取类
  8. STL中map与hash_map容器的选择
  9. 看看这段代码有没有内存泄露?
  10. Spring的bean管理(注解注入属性)