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. ...
随机推荐
- 递归打印lua中的table
在lua中,table是比较常用的数据形式,有时候为了打印出里面的内容,需要做一些特殊处理. 废话不多讲,直接粘代码: print = release_print -- 递归打印table local ...
- HTML的Tomcat
修改D:\software\apache-tomcat-8.0.44\webapps\ROOT\WEB-INF\web.xml: <?xml version="1.0" en ...
- UTF-8的BOM含义
BOM的介绍 在github上写md文件的时候,发现生成自己blog时,报出一个错误是让使用UTF-8编码,然后在Notepad++上把文件转成UTF-8时,发现菜单中有"UTF-8无BOM ...
- 15. Life Cycle of the Products 产品的生命周期
15. Life Cycle of the Products 产品的生命周期 (1) We can see how the product life cycle works by looking at ...
- s6-4 TCP 数据段
传输控制协议 TCP (Transmission Control Protocol) 是专门为了在不可靠的互联网络上提供可靠的端到端字节流而设计的 TCP必须动态地适应不同的拓扑.带宽.延迟. ...
- 谈谈websocket集群的解决方式
上文我们已经利用websocket实现微信二维码支付的业务. 上述实现在单机环境中实现是没有什么问题的,无非就是客户端连接服务端,首先将连接的websocketsession存在一个map里面,当异步 ...
- PID control
|—平滑化算法 |—PID控制—|—P控制器编程 |—PD控制编程 |—PID控制编程 |—参数优化 |—实验P.PD.PID对减小系统误差的作用 这里讨论怎么将路径转变成行动指令(生成平滑的路径), ...
- Touch365现已上架!
欢迎体验由武宇亭.诸子轩.梁国伟.张裕浩.孔维喆.邱亚威同学开发的创意照片浏览软件Touch365,现已上架Microsoft官方商城! https://www.microsoft.com/zh-cn ...
- 快速入门Vue
前端技术发展很快,近日一个项目中想用Vue框架,对此对Vue基础进行了一些学习整理 何为Vue,官网 解释Vue.js(读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式框架 这里记录 ...
- pyqt5 graphics view简单使用
Graphics View提供了一个平面,用于管理和交互大量自定义的2D图形图元,以及一个用于可视化图元的视图窗口小部件,支持缩放和旋转. 该框架包括一个事件传播架构,允许场景中图元的精确双精度交互功 ...