import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/permutations-ii/
*
* Created by lverpeng on 2017/7/17.
*
* Given a collection of numbers that might contain duplicates, return all possible unique permutations.
*
* For example,
* [1,1,2] have the following unique permutations:
* [1,1,2], [1,2,1], and [2,1,1].
*
*/
public class Permutation2 { /**
* 找出数组元素可以组成的所有排列
*
* 递归求出每一个元素开头的组合
*
*
* @param arr
* @return
*/
public void permute (int[] arr, int start, int end, List<int[]> result) {
if (start >= end-1) {int[] newArr = new int[arr.length];
System.arraycopy(arr,0, newArr, 0, arr.length);
result.add(newArr); return ;
} for (int i = start; i < end; i++) {
if (i < end - 1 && arr[i] == arr[i + 1]) {
// 如果是相同的元素,则跳过当前元素
continue;
}
swap(arr, start, i);
permute(arr, start + 1, end, result);
swap(arr, i, start);
}
} private void swap (int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
} public static void printList (List<int[]> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(Arrays.toString(list.get(i)));
}
} public static void main(String[] args) {
Permutation2 permutation2 = new Permutation2();
int[] arr = new int[]{1,2,1};
Arrays.sort(arr);
int[] arr1 = new int[]{1,3,2};
Arrays.sort(arr1);
List<int[]> result = new ArrayList<int[]>();
permutation2.permute(arr, 0, arr.length, result);
printList(result);
System.out.println();
result = new ArrayList<int[]>();
permutation2.permute(arr1, 0, arr1.length, result);
printList(result);
}
}

leetcode — permutations-ii的更多相关文章

  1. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  2. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  3. [LeetCode] Permutations II 全排列之二

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

  4. [leetcode]Permutations II @ Python

    原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...

  5. leetcode -- Permutations II TODO

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

  6. [Leetcode] Permutations II

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

  7. [Leetcode] permutations ii 全排列

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

  8. [LeetCode] Permutations II 排列

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

  9. LeetCode Permutations II (全排列)

    题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...

  10. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

随机推荐

  1. 第四周助教工作总结——NWNU李泓毅

    1.    助教博客链接: https://www.cnblogs.com/NWNU-LHY/ 2.    作业要求链接: www.cnblogs.com/nwnu-daizh/p/10487329. ...

  2. 01 C语言程序设计--01 C语言基础--第1章 C语言概述&第2章 GCC和GDB

    走进嵌入式开发的世界,企业级项目课程让你达到企业嵌入式应用开发要求.名师在线答疑,解决疑难.科学评测体系,系统评估学习.核心项目实........ 30 门课程 241小时12分钟 824 人学习 学 ...

  3. shiro简单配置 (写的不错 收藏一下)

    抄袭的连接:https://blog.csdn.net/clj198606061111/article/details/24185023 注:这里只介绍spring配置模式. 因为官方例子虽然中有更加 ...

  4. 函数append()和html()的区别

    它们的功能缺失有点相似,但是实际上本质上是有区别: append()函数是为指定元素尾部附加内容,而html()函数是重置元素内部的html内容.·

  5. Spring资源加载器抽象和缺省实现 -- ResourceLoader + DefaultResourceLoader(摘)

    概述 对于每一个底层资源,比如文件系统中的一个文件,classpath上的一个文件,或者一个以URL形式表示的网络资源,Spring 统一使用 Resource 接口进行了建模抽象,相应地,对于这些资 ...

  6. (PMP)第3章-----项目经理的角色

    项目经理的能力: 1.技术项目管理 2.领导力 3.战略和商务管理 ----------------------------------------------- 管理:指挥从一个位置到另一个位置 领 ...

  7. ES6使用fetch请求数据

    ie是完全不支持fetch的. fetch(url,{method:"get/post"}).then(res=>{   }) 如果请求返回的status是200,body是 ...

  8. WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@584] - Cannot open channel to 4 at election address Slave3.Hadoop/xxx.xxx.xxx.xxx

    这些日子为这个错误苦恼很久了,网上找到的各种方法都试了一遍,还是没能解决. 安装好zookeeper后,运行zkServer.sh start 显示正常启动,但运行zkServer.sh status ...

  9. fortran常用语句--读写带注释文档、动态数组等语法

    1.判断读取文档有多少行数据(文档最后的空行不计入其中): 首先在变量定义区域下方和执行语句前声明在程序中要被调用的GetFileN函数: external GetFileN 接下来在函数外部后边写上 ...

  10. bootstrap table使用参考

    https://www.cnblogs.com/landeanfen/p/5821192.html  转载 阅读目录 一.x-editable组件介绍 二.bootstrapTable行内编辑初始方案 ...