leetcode — permutations-ii
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的更多相关文章
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- leetcode -- Permutations II TODO
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] permutations ii 全排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode Permutations II (全排列)
题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
随机推荐
- 使用java开发spark的wordcount程序(多种实现)
package spark; import org.apache.spark.SparkConf; import org.apache.spark.api.java.JavaPairRDD; impo ...
- SJCP认证题前五十题填坑
在做Java的SJCP认证试题时自己整理了一些Java基础细节知识点,以下是知识点陈列 1.标签机制:标签起作用的唯一的地方刚好在迭代语句之前(不然编译错误) continue label1 直接转到 ...
- 出错with root cause
[背景:] 我自己写了一个项目,主页可以看到一个数据库里的一个应用的users用户表的所有数据,包括用户的年龄,姓名,出生日期等信息.后来又想再增加一个注册功能,写好了之后进行单元测试,结果就出现了w ...
- 随便写写,也有一些参考了我jio的很好的他人的成果
Spring框架学习记录(1) 一. https://www.cnblogs.com/yuanqinnan/p/10274934.html (一)只要用框架开发java,一定躲不过spring,Spr ...
- JS获取form表单数据
以下代码可放在一个js文件中,以便通用: //获取指定表单中指定标签对象 function getElements(formId, label) { var form = document.getEl ...
- Jeecg框架简介
官方地址:http://www.jeecg.org/
- 05 详解C# 迭代器
迭代器模式是设计模式中行为模式(behavioral pattern)的一个例子,他是一种简化对象间通讯的模式,也是一种非常容易理解和使用的模式. 简单来说,迭代器模式使得你能够获取到序列中的所有元素 ...
- [转] KVM虚拟化技术生态环境介绍
KVM虚拟化技术生态环境介绍 http://xanpeng.github.io/wiki/virt/kvm-virtulization-echosystem-intro.html kvm和qemu/q ...
- 【.NET Core项目实战-统一认证平台】第一章 功能及架构分析
[.NET Core项目实战-统一认证平台]开篇及目录索引 从本文开始,我们正式进入项目研发阶段,首先我们分析下统一认证平台应该具备哪些功能性需求和非功能性需求,在梳理完这些需求后,设计好系统采用的架 ...
- navicat 几个 可用的东西
1.常用的 表格 一启动 就进入的某某连接某某数据库某某表 2. 结构 比对(菜单栏 “工具里面”) 3.数据对比 同上 4.保持连接 5.全局查询 在工具中查找 ------在数据库或模式中查找