解析:

一:非字典序(回溯法)

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,字典序 + 非字典序排列)的更多相关文章

  1. java和javax都是Java的API包,java是核心包,javax的x是extension的意思,也就是扩展包。

    java和javax都是Java的API包,java是核心包,javax的x是extension的意思,也就是扩展包.

  2. Servlet和JSP中的过滤器都是Java类

    JSP 过滤器 Servlet和JSP中的过滤器都是Java类,它们存在的目的如下: 在请求访问后端资源时拦截它 管理从服务器返回给客户端的响应 下面列出了多种常用的过滤器类型: 认证过滤器 数据压缩 ...

  3. 为什么大家都说Java中只有值传递?

    最近跟Java中的值传递和引用传递杠上了,一度怀疑人生.查了很多资料,加上自己的理解,终于搞清楚了,什么是值传递和引用传递.也搞明白了,为什么大家都说Java只有值传递,没有引用传递.原来,我一直以来 ...

  4. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  5. 46. 47. Permutations

    求全排列. 1. 无重复元素 Given a collection of distinct numbers, return all possible permutations. For example ...

  6. [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...

  7. “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...

  8. Permutations and Permutations II

    Permutations 问题:给定一个无重复元素的数组,输出其中元素可能的所有排列 示例: 输入:[2,3,4] 输出:[ [2,3,4], [2,4,3], [3,2,4], [3,4,2], [ ...

  9. 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 ...

随机推荐

  1. Docker Tomcat远程部署到容器

    一:创建一个开启远程部署的tomcat容器 tomcat角色配置 1.tomcat开启远程部署,修改conf/tomcat-users.xml <?xml version="1.0&q ...

  2. 题解——ATCoder AtCoder Grand Contest 017 B - Moderate Differences(数学,构造)

    题面 B - Moderate Differences Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stat ...

  3. MPU6050

    MPU6050: Pitch,Roll,Yaw旋转方向遵循右手定则 pith角  –绕Y轴(俯仰)  范围:±90°  ,与旋转方向相反转是增大   -- 抬头为正,低头为负 roll角  –绕X轴( ...

  4. java中垃圾回收算法讲解

      判断对象是否存活的方法: 一.引用计数算法(Reference Counting) 介绍:给对象添加一个引用计数器,每当一个地方引用它时,数据器加1:当引用失效时,计数器减1:计数器为0的即可被回 ...

  5. git Bush应用崩溃If no other git process is currently running, this probably means a git process crashed

    问题: 用git Bush提交的时候遇到一个问题,不论做什么操作都遇到下面的错误信息: fatal: Unable to create 'XXXXXXXXX' : File exists. If no ...

  6. echo -n 和echo -e 参数意义

    echo -n 不换行输出 $echo -n "123" $echo "456" 1 2 最终输出 123456 而不是 123 456 1 2 3 4 5 6 ...

  7. 【Python】【有趣的模块】【sys&time&os】

    [模块] sys.path.append('C:/Users/wangxue1/PycharmProjects/selenium2TestOne') 然后就可以直接import 这个路径下的模块了 [ ...

  8. 【BZOJ】3527: [Zjoi2014]力

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3527 把${f_i}$消去之后换元就是卷积的形式,直接算就可以了. #include< ...

  9. IntelliJ IDEA Tomcat中端口被占用的问题

    早上来公司,新建了一个项目,启动Tomcat,报错,如图所示 端口1099被占用 cmd——netstat -aon|findstr 1099 此时出现了一个问题,输入:netstat -an,提示: ...

  10. python函数的动态传参.作用域与命名空间

    一.动态传参1.*表示动态传参. 可以接受所有的位置参数传参的时候自动的把实参打包成元组 交给形参 def chi(*food): print(food) chi() # 动态传参可以不传参数 chi ...