46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)
解析:
一:非字典序(回溯法)
1)将第一个元素依次与所有元素进行交换;
2)交换后,可看作两部分:第一个元素及其后面的元素;
3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素时,得到一个排列;
4)将第1步中交换的元素还原,再与下一个位元素交换。
重复以上4步骤,直到交换到最后一个元素。(剑指offer中也有例题讲解)
排除重复:在for循环中,从start开始,每次与当前的i位置元素交换,每次交换前,
需要判断start到i之间(不包括i)是否具有同i位置相同的元素,若存在,则跳过该轮循环,从而避免重复排列的产生。
二:字典序,该方法本身具有去除重复的作用。
根据当前序列,生成下一个序列,交换 + 逆序。当得到一个排列中所有元素已经逆序时,说明这是最后一个排列了。
代码:
一:非字典序(回溯法)
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义数组
int[] array = {1,1,3};
List<List<Integer>> list = new ArrayList<List<Integer>>();
int start = 0;
//去重
list = getAllPermutations(array, new ArrayList<List<Integer>>(), start);
System.out.println(list);
}
/**
* 方法一:非字典序
* 判断当前要交换的字符在前面是否已经出现过,若已经出现过,则不交换
*
* @param array
* @return
*/
public static List<List<Integer>> getAllPermutations(int[] array,List<List<Integer>> list,int start){
if(start == array.length){//遍历到最后一个
List<Integer> item = new ArrayList<Integer>(array.length);
for(int i = 0,len = array.length; i < len; ++i)
item.add(array[i]);
list.add(item);
}
for(int i = start,len = array.length; i < len; ++i){
boolean flag = false;
for(int j = start; j < i; ++j) //i是待交换元素,start到i之间(不包括i),若出现过同i相同的元素,则不再交换
if(array[j] == array[i])//若存在重复数字,则不交换
flag = true;
if(!flag){
swap(array,i,start);
//递归在循环里
getAllPermutations(array,list,start + 1);
//回溯
swap(array,i,start);
}
}
return list;
}
/**
* 交换元素
* @param array
* @param i
* @param j
*/
public static void swap(int[] array,int i ,int j){
//交换
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
二、字典序
/**
* 字典序
* @return
*/
public static List<List<Integer>> permutations(int[] array,List<List<Integer>> list){
//将原数组加入结果集
List<Integer> item = new ArrayList<Integer>();
for(int j = 0,l = array.length; j < l; ++j)
item.add(array[j]);
list.add(item);
while(!isLastOne(array)){
list.add(nextPermutation(array));
}
return list;
} /**
* 判断数组元素是否逆序,若已经逆序,说明已经是全部排列的最后一个
* @param array
* @return
*/
public static boolean isLastOne(int[] array){
for(int i = 0; i < array.length - 1; ++i){
if(array[i] < array[i + 1])
return false;
}
return true;
} /**
* 获取下一个字典序的排列
* @return
*/
public static List<Integer> nextPermutation(int[] nums){
if(nums == null)
return null;
if(nums.length == 0)
return new ArrayList<Integer>();
//长度为1的数组
if (nums.length == 1) {
return new ArrayList<Integer>(nums[0]);
}
//存储结果
List<Integer> result = new ArrayList<Integer>(); //从后向前找到第一个不满足逆序的元素
int i = nums.length - 2;
for(; i >= 0 && nums[i] >= nums[i + 1]; --i); //注意,这里有=,可以排除含有重复元素的情况 //从i+1位置开始,向后查找比nums[i]大的最小元素
if(i >= 0){
int j = i + 1;
for(; j < nums.length && nums[j] > nums[i]; ++j);
swap(nums,i,j - 1); //交换,注意是同 j - 1交换
} //将i之后的元素逆置(这里包含一种特殊情况,若该排列已经是字典序中的最大了,则下一个序列应该是最小字典序,因此,直接在这里逆置即可)
int k = nums.length - 1;
i++;
for(; i < k; i++, k--)
swap(nums, i, k); for(int l = 0,len = nums.length; l < len; ++l)
result.add(nums[l]); return result;
} /**
* 交换元素
* @param array
* @param i
* @param j
*/
public static void swap(int[] array,int i ,int j){
//交换
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)的更多相关文章
- java和javax都是Java的API包,java是核心包,javax的x是extension的意思,也就是扩展包。
java和javax都是Java的API包,java是核心包,javax的x是extension的意思,也就是扩展包.
- Servlet和JSP中的过滤器都是Java类
JSP 过滤器 Servlet和JSP中的过滤器都是Java类,它们存在的目的如下: 在请求访问后端资源时拦截它 管理从服务器返回给客户端的响应 下面列出了多种常用的过滤器类型: 认证过滤器 数据压缩 ...
- 为什么大家都说Java中只有值传递?
最近跟Java中的值传递和引用传递杠上了,一度怀疑人生.查了很多资料,加上自己的理解,终于搞清楚了,什么是值传递和引用传递.也搞明白了,为什么大家都说Java只有值传递,没有引用传递.原来,我一直以来 ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- 46. 47. Permutations
求全排列. 1. 无重复元素 Given a collection of distinct numbers, return all possible permutations. For example ...
- [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...
- “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...
- Permutations and Permutations II
Permutations 问题:给定一个无重复元素的数组,输出其中元素可能的所有排列 示例: 输入:[2,3,4] 输出:[ [2,3,4], [2,4,3], [3,2,4], [3,4,2], [ ...
- LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...
随机推荐
- 马虎的算式|2013年蓝桥杯B组题解析第二题-fishers
小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了. 有一次,老师出的题目是:36 x 495 = ? 他却给抄成了:396 x 45 = ? 但结果却很戏剧性,他的答案竟然是对的!! 假设 ...
- Logstash Introduction
https://www.cnblogs.com/aresxin/p/8035137.html Elasticsearch是个开源分布式搜索引擎,提供搜集.分析.存储数据三大功能.它的特点有:分布式,零 ...
- 【特性】Redis4.0新特性
模块系统 Redis 4.0 发生的最大变化就是加入了模块系统, 这个系统可以让用户通过自己编写的代码来扩展和实现 Redis 本身并不具备的功能, 具体使用方法可以参考 antirez 的博文< ...
- js 通过id或class获得的对象说明
通过id获取的是一个对象 通过class获取的是一个数组 $($(".layui-tab-item layui-show")[0]).html(data)//实际测试没用. ...
- 题解——Codeforces Round #507 (based on Olympiad of Metropolises) T2(模拟)
T2还是模拟 枚举一下第一个放哪里 然后贪心的反转即可 虽然我也不会证,但是这题肯定有解qwq #include <cstdio> #include <algorithm> # ...
- using Redis in .net core
Using Redis Cache in .net Core Distributed Cache using Redis and ASP.NET Core ASP.NET Core Data Prot ...
- PowerDesigner使用方法入门学习
[转载:https://www.cnblogs.com/biehongli/p/6025954.html] 1:入门级使用PowerDesigner软件创建数据库(直接上图怎么创建,其他的概念知识可自 ...
- Java基础 --Unix与Mac系统 文件路径分隔符(一)
斜杠‘/’与反斜杠‘\’在不同系统的使用 1)Window平台使用反斜杠'\'作为文件层级分隔符:Windows使用反斜杠作为DOS命令提示符的参数标志,随着发展DOS命令符逐渐被淘汰,大部分情况下斜 ...
- hibernate的三种状态和缓存
hibernate的三种状态: 1.瞬时态:对象里面没有id值,对象与session没有关联 类似,把class类new出来,不存进session 2.持久态:对象里面有id值,对象与session关 ...
- 学习笔记49—matlab FDR校正
matlab自带函数mafdr,当ttest数较多时,可直接用[FDR, Q]=mafdr(P):但是Storey procedure在p值少于1000个时会崩溃,此时应改用BH FDR方法:mafd ...