无重复

[抄题]:

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].

[思维问题]:

不知道回溯法:和求子集一样-[1],[1,2]-只剩[1]

123用了2,312又要用2。反复用,所以叫回溯法

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 递归退出用return;表示 递归推出的出口在helper函数中,不在每个点加进permutation的过程中
  2. 结果是复数数组,返回results 如果nums == null,返回[] 如果nums.length == 0, 返回[[]](result中添加一个空数组,返回result)
  3. ist.size()的效果等于nums.length
  4. helper调用整个数组时,参数是数组名

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 搜索递归的三个步骤:新参数、递归、去掉新参数
  2. 不知道回溯法:和求子集一样-[1],[1,2]-只剩[1]

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
//corner case
List<List<Integer>> results = new ArrayList<>();
List<Integer> permutations = new ArrayList<>();
HashSet<Integer> set = new HashSet<>(); if (nums == null) {
return null;
}
if (nums.length == 0) {
//return new ArrayList<>();
results.add(new ArrayList<>());
return results;
}
//helper
helper(nums, permutations, set, results);
//return
return results;
}
//helper
public void helper (int[] nums, List<Integer> permutations,
HashSet<Integer> set, List<List<Integer>> results) {
if (permutations.size() == nums.length) {
results.add(new ArrayList<>(permutations));
return ;//
} for (int i = 0; i < nums.length; i++) {
if (set.contains(nums[i])) {
continue;
}
permutations.add(nums[i]);
set.add(nums[i]);
helper(nums, permutations, set, results);
set.remove(nums[i]);
permutations.remove(permutations.size() - 1);
}
}
}

有重复

[抄题]:

[思维问题]:

有重复元素不知道是默认不排序的,要先排序

[一句话思路]:

排序后用visited数组来控制

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

((i != 0 && visited[i - 1] == 0) &&
nums[i - 1] == nums[i]//
)

在不是第零位(没有前一位)的前提下,如果两数字相同,前面一个数却没有访问,此时不可

[一刷]:

  1. 有数组要先排序
  2. 设置visited数组默认为0

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

不要把visited nums数组名写错了

[总结]:

代码风格:太长可以换行、不要用很多的&& ||符号

[复杂度]:Time complexity: O(分支的深度次方) Space complexity: O(深度*分支)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

31. Next Permutation 如果不是要求找出全部排列的,就几乎都是做数组调整 玩文字游戏

public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permuteUnique(int[] nums) {
//corner case
List<List<Integer>> results = new ArrayList<>();
List<Integer> permutations = new ArrayList<>();
int[] visited = new int[nums.length]; if (nums == null) {
return null;
}
if (nums.length == 0) {
results.add(new ArrayList<>());
return results;
}
//helper
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
visited[i] = 0;
}
helper(nums, permutations, visited, results);
//return
return results;
}
//helper
public void helper (int[] nums, List<Integer> permutations,
int[] visited, List<List<Integer>> results) {
if (permutations.size() == nums.length) {
results.add(new ArrayList<>(permutations));
return ;//
} for (int i = 0; i < nums.length; i++) {
if (visited[i] == 1) {
continue;
}
if ((i != 0 && visited[i - 1] == 0) &&
nums[i - 1] == nums[i]//
) {
continue;
}
visited[i] = 1;
permutations.add(nums[i]);
helper(nums, permutations, visited, results);
permutations.remove(permutations.size() - 1);
visited[i] = 0;
}
}
}

全排列12 · Permutations的更多相关文章

  1. Leetcode之回溯法专题-46. 全排列(Permutations)

    Leetcode之回溯法专题-46. 全排列(Permutations) 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3, ...

  2. [Swift]LeetCode47. 全排列 II | Permutations II

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

  3. LeetCode 46. 全排列(Permutations)

    题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [ ...

  4. 刷题——有重复元素的全排列(Permutations II)

    题目如上所示. 我的解决方法(参考了九章的答案!): class Solution { public: /* * @param : A list of integers * @return: A li ...

  5. [CareerCup] 9.5 Permutations 全排列

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

  6. LeetCode 046 Permutations

    题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...

  7. HDOJ-ACM1016(JAVA) 字典序全排列,并剪枝

    转载声明:原文转自http://www.cnblogs.com/xiezie/p/5576273.html 题意: 一个环是用图中所示的n个圆组成的.把自然数1.2.…….n分别放入每个圆中,并在相邻 ...

  8. 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>

    """给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...

  9. UVALive 6909 Kevin's Problem 数学排列组合

    Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...

随机推荐

  1. 搭建GlusterFS文件系统

    (1)环境准备 创建两个虚拟机配置如下 把仅主机第二张网卡配置如下: GlusterFS1 GlusterFS2 上传文件到opt目录下 文件内容如下 (2)GlusterFS安装配置 1.安装Glu ...

  2. Linux中的ls命令详细使用

    ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,下面我们就来一起看看ls的用法 英文全名:List即列表的意思,当我们学习某种东西的时候要做到知其所 ...

  3. https单向认证服务端发送到客户端到底会不会加密?

    最近向大神请教了CA认证单向认证和双向认证的问题,有一点不太明白,单向认证的时候,为什么服务端发送到客户端的东西也是加密的?

  4. 【Codeforces】CF 911 D. Inversion Counting(逆序对+思维)

    题目 传送门:QWQ 分析 思维要求比较高. 首先我们要把原图的逆序对q算出来. 这个树状数组或归并排序都ok(树状数组不用离散化好评) 那么翻转$[l,r]$中的数怎么做呢? 暴力过不了,我试过了. ...

  5. QEMU,KVM及QEMU-KVM介绍

    What's QEMU QEMU是一个主机上的VMM(virtual machine monitor),通过动态二进制转换来模拟CPU,并提供一系列的硬件模型,使guest os认为自己和硬件直接打交 ...

  6. 53. sql2005“备份集中的数据库备份与现有的xx数据库不同”解决方法

    RESTORE DATABASE LiveBOS_KeJiFROM DISK = 'D:\LiveBOS_KeJi_backup_201503090000.bak' --bak文件路径with rep ...

  7. angular 中怎么获取路径上的参数 参考:https://docs.angularjs.org/api/ng/service/$location

    参考: https://docs.angularjs.org/api/ng/service/$location

  8. leetcode929

    package main import ( "fmt" "strings" ) func numUniqueEmails(emails []string) in ...

  9. as3 文本竖排效果实现

    import flash.text.engine.TextBlock; import flash.text.engine.ElementFormat; import flash.text.engine ...

  10. 前端-CSS-10-定位

    <!-- 定位有三种: 1.相对定位 2.绝对定位 3.固定定位 这三种定位,每种定位都暗藏玄机,所以我们要一一单讲 position:relative; position:absolute; ...