leetcoe--47. Permutations II
1、问题描述
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],
[2,1,1]
]
2、边界条件:重复数字,去重方法是重要考察点,采用事前去重是有效率的。
3、思路:排列问题,递归的方法实现,先取一个数放在位置1,然后剩下N-1个位置,再依次放入;循环,取第二个数放在位置1。
4、代码实现
方法一
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> results = new ArrayList<>();
Arrays.sort(nums);
List<Integer> numList = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
numList.add(nums[i]);
}
permuteUnique(results, new ArrayList<Integer>(), numList);
return results;
}
public void permuteUnique(List<List<Integer>> results, List<Integer> cur,
List<Integer> numList) {
if (0 == numList.size()) {
List<Integer> result = new ArrayList<>(cur);
results.add(result);
return;
}
for (int i = 0; i < numList.size(); i++) {
if (i != 0 && numList.get(i) == numList.get(i - 1)) { //事前去重
continue;
}
cur.add(numList.get(i));
numList.remove(i);
permuteUnique(results, cur, numList);
numList.add(i, cur.get(cur.size() - 1));
cur.remove(cur.size() - 1);
}
}
}
方法二
优化一下数据结构
leetcoe--47. Permutations II的更多相关文章
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II (Back-Track, Sort)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II (JAVA)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
随机推荐
- debian服务查询
1.查询 用root身份执行service --status-all查看所有服务的状态 "+" started "-" stopped "?" ...
- JQUERYUI 框架 http://jqueryui.com/
http://jqueryui.com/
- 【转】 Pro Android学习笔记(七三):HTTP服务(7):AndroidHttpClient
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog.csdn.net/flowingflying/ 不知道此文是否是这个系列中最短的一篇.我们 ...
- 【转】Pro Android学习笔记(八):了解Content Provider(下中)
在之前提供了小例子BookProvider,我们回过头看看如何将通过该Content Provider进行数据的读取. (1)增加 private void addBook(String name , ...
- 1 slow requests are blocked > 32 sec解决方法
[root@node1 ~]# ceph -s cluster: id: b8b4aa68-d825-43e9-a60a-781c92fec20e health: HEALTH_WARN Reduce ...
- C语言 mmap()函数(建立内存映射) 与 munmap()函数(解除内存映射)
mmap将一个文件或者其它对象映射进内存.文件被映射到多个页上,如果文件的大小不是所有页的大小之和, 最后一个页不被使用的空间将会清零.mmap在用户空间映射调用系统中作用很大. 条件 mmap()必 ...
- 启动新内核出现:No filesystem could mount root, tried: ext3 ext2 cramfs vfa
转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/51841791 下载新编译的内核出现:No filesystem could mou ...
- Java enum(枚举)使用详解之二
enum 对象的常用方法介绍 int compareTo(E o) 比较此枚举与指定对象的顺序. Class<E> getDeclaringClass() ...
- PopupWindow 防微信弹出右 侧窗体(继承PopupWindow )
1, pop自定义 public class SelectPicPopupWindow extends PopupWindow { private Button btn_take_photo, btn ...
- Windows环境下 Hadoop Error: JAVA_HOME is incorrectly set. 问题
最近尝试在windows开发MR程序并且提交Job,在解压缩好Hadoop,配置好环境变量后, 打开cmd 输入hadoop version 的时候出现以下错误: Error: JAVA_HOME i ...