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. 使用kbmmw 的REST 服务实现上传大文件

    我们在使用kbmmw的REST 服务时,经常会下载和上传大文件.例如100M以上的.kbmmw的rest服务中 提供标准的文件下载,上传功能,基本上就是打开文件,发送,接收,没有做特殊处理.这些对于文 ...

  2. Unity3D中AssetBundle应用

    工程中的模型等资源转化为Prefab后,打包成AssetBundle,可以大幅降低资源的空间占有度,并且提高资源加载的效率. 一.AssetBundle的打包 先看下打包Prefab的脚本代码,这段脚 ...

  3. MySQL数据库(四)多表查询

    两张假设有两张表格A和B,把表格当作一个集合,那么表格中的记录就是集合中的一个元素. 两张表格如下: TableA:TableB: 2.1 内连接(只有一种场景) inner join 或者join( ...

  4. python property对象

    一.从@porperty说起 Python内置的@property装饰器是负责把一个方法变成属性调用的 class Stu(object): def __init__(self,age): self. ...

  5. 中触发一个断点 其原因可能是堆被损坏,这说明 ***.exe 中或它所加载的任何 DLL 中有 Bug

    软件中使用了DevComponents.DotNetBar2.dll MessageBoxEx.Show("ddd");运行到这句出现这个错误 : 中触发一个断点 其原因可能是堆被 ...

  6. 我的C#跨平台之旅(六):发布应用

    由于此架构从一开始就将.NET Framework 的依赖降低到最低,且不依赖IIS,在ORM层面,完全实现代码优先,即真正做到数据库无关: Windows服务器部署: 在Windows应用服务器上安 ...

  7. 2018/9/6 spring框架的整理

    spring知识的巩固整理AOP和ioc概念,以及了解到了为何要使用spring框架的目的,作用:变换资源获取的方向.更像是按需所求.配置bean的方式:利用XML的方式,基于注解的方式两种.1通过全 ...

  8. python基础自学 第二天

    注释 分类 单行注释 多行注释 作用 使用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序可读性 单行注释(行注释) 以 # 开头,#右边所有的东西就被当成说明文字,而不是要执行的程序,只是说 ...

  9. mysql case when then else end 的写法

    select t.colum1,t.colum2 (case when t.colum1 = '' then '' t.when colum2 = '' then '' else '' end ) a ...

  10. java实现中缀表达式转后缀表达式

    package postfix; import java.util.Stack; /** * * @author DELL 将中缀表达式转化为后缀表达式 */ public class Express ...