全排列12 · Permutations
无重复
[抄题]:
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。反复用,所以叫回溯法
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 递归退出用return;表示 递归推出的出口在helper函数中,不在每个点加进permutation的过程中
- 结果是复数数组,返回results 如果nums == null,返回[] 如果nums.length == 0, 返回[[]](result中添加一个空数组,返回result)
- ist.size()的效果等于nums.length
- helper调用整个数组时,参数是数组名
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 搜索递归的三个步骤:新参数、递归、去掉新参数
- 不知道回溯法:和求子集一样-[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]//
)
在不是第零位(没有前一位)的前提下,如果两数字相同,前面一个数却没有访问,此时不可
[一刷]:
- 有数组要先排序
- 设置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的更多相关文章
- Leetcode之回溯法专题-46. 全排列(Permutations)
Leetcode之回溯法专题-46. 全排列(Permutations) 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3, ...
- [Swift]LeetCode47. 全排列 II | Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 46. 全排列(Permutations)
题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [ ...
- 刷题——有重复元素的全排列(Permutations II)
题目如上所示. 我的解决方法(参考了九章的答案!): class Solution { public: /* * @param : A list of integers * @return: A li ...
- [CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...
- LeetCode 046 Permutations
题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...
- HDOJ-ACM1016(JAVA) 字典序全排列,并剪枝
转载声明:原文转自http://www.cnblogs.com/xiezie/p/5576273.html 题意: 一个环是用图中所示的n个圆组成的.把自然数1.2.…….n分别放入每个圆中,并在相邻 ...
- 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>
"""给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...
- UVALive 6909 Kevin's Problem 数学排列组合
Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...
随机推荐
- Unreal Engine 4 性能优化工具(Profiler Tool)
转自:http://aigo.iteye.com/blog/2296548 Profiler Tool Reference https://docs.unrealengine.com/latest/I ...
- Mybatis 接口绑定
MyBatis的接口绑定: 参考链接:http://blog.csdn.net/chris_mao/article/details/48836039 接口映射就是在IBatis中任意定义接口,然后把接 ...
- nginx-1.8.1的安装
1.我直接切换到root用户下安装,这里需要三个插件一起配套使用的 分别是: 1.gzip 模块需要 zlib 库 ( 下载: http://www.zlib.NET/ )2.rewrite 模块需要 ...
- Map 合并
比如说 qq.com 100 163.com 90 QQ.COM 10 Qq.Com 5 …… 如果统计的话,需要忽略大小写的,即 QQ邮箱总共是100+10+5,怎么写? 其实这个应该不难的,就 ...
- Hibernate 和 MyBatis 的对比
一.开发对比 开发速度 Hibernate 的真正掌握要比MyBatis来的难些.MyBatis框架较轻量级,相对简单很容易上手,但也相对简陋些.个人觉得要用好 MyBatis 还是要首先理解好 Hi ...
- jenkins API
1.curl http://199.168.299.99:8080/job/send_message/lastBuild/api/json --user administrator:1234 获取j ...
- python入门-变量和简单数据类型
1 title() 是以首字母大写的方式显示每个单词 lower() 字母小写 upper() 字母大写 2 python使用+号来合并字符串 字符串中使用制表符用\t 字符串中使用换行符\n 用rs ...
- mock——test 基本所有使用
可以参考:http://www.cnblogs.com/lyy-2016/p/6122144.html test /** * */ package com.imooc.web.controller; ...
- redis详解(三)
1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...
- shiro与threamleaf的整合
1.添加依赖 2.在配置类中添加shiroDialect