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. tensorflow学习之(十一)RNN+LSTM神经网络的构造

    #RNN 循环神经网络 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data tf.se ...

  2. Vue-箭头函数

    03-箭头函数   箭头函数 基本语法: ES6允许使用“箭头”(=>)定义函数 var f = a = > a //等同于 var f = function(a){ return a; ...

  3. HDU 6377 度度熊看球赛 (计数DP)

    度度熊看球赛 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  4. Lambda表达式遍历和泛型ForEach遍历方式

    lambda表态式 DataTable dtAllItems = policySecurity.GetUserAccessTypeOnAllItems(userID); List<DataRow ...

  5. ABAP接口之Http发送json报文

    abap 调用http 发送 json 测试函数 SE11创建结构:zsmlscpnotice SE37创建函数:zqb_test_http_fuc1 FUNCTION zqb_test_http_f ...

  6. Object constraint language for code generation from activity models

    一.基本信息 标题:Object Constraint Language for Code Generation from Activity Models 时间:2018 出版源:Informatio ...

  7. latex 希腊字母表示

    http://blog.sina.com.cn/s/blog_5e16f1770100lxq5.html

  8. SQL给数据编号

    方法:ROW_NUMBER() over(order by getdate()) AS num 使用案例 : select * From (select  ROW_NUMBER() over(orde ...

  9. JavaScript基础整理

    在HTML页面中嵌入JavaScript代码的三种方式 1.内嵌 写在<head>标签或<body>标签中 <script> function displayTim ...

  10. Scala中的Implicit详解

    Scala中的implicit关键字对于我们初学者像是一个谜一样的存在,一边惊讶于代码的简洁, 一边像在迷宫里打转一样地去找隐式的代码,因此我们团队结合目前的开发工作,将implicit作为一个专题进 ...