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 ... 
随机推荐
- git常见使用情境整理
			一.版本回退 回退到某个commit版本的方法如下: 1. 查看commit历史 git log 找到想要回退的版本的号码,eg:f765889 2. 回退到该版本 git reset f765889 ... 
- IP  地址漂移
			1.概念 应用访问虚拟ip,当主服务器正常工作时,虚拟ip指向主服务器,当主服务器宕掉后,虚拟ip自动指向从服务器,当主服务器被人修好后,再自动指向主服务器, 这种虚拟ip的指向方式称为ip地址漂移. ... 
- 简单的RBAC用户角色权限控制
			Java web项目中,无论项目是大是小,或多或少都会涉及到用户访问权限的控制,权限管理总体的设计思路就是,不该看的不看,不该做的不做!据我目前的了解,我所知道的几种实现访问权限控制的方式有: JQu ... 
- Hdu 4762 网络赛 高精度大数模板+概率
			注意题目中的这句话he put the strawberries on the cake randomly one by one,第一次选择草莓其实有N个可能,以某一个草莓为开头,然后顺序的随机摆放, ... 
- word2010以上版本中快捷录入数学公式的方法(二)
			以前推荐的方法,随着方正飞翔网站上关闭了数学公式输入法的支持也不能不用了,现在再推荐一个可以在word2010以上版中快捷输入数学公式的方法,安装AxMath,一切问题都OK!我是直接购买的正版,25 ... 
- 删除 char[10][10] 中的一行
			1. 描述 删除二维字符数组其中一行,并用下一行进行填补 2. 代码 #include <iostream> #include <string.h> using namespa ... 
- vs2015类中方法前的引用链接不显示的解决方案
			在工具→选项,打开如下界面,寻找“文本编辑器→所有语言”中设置显示:<img data-rawheight="761" data-rawwidth="130 ... 
- WPF 界面提示加载出错
			当WPF界面代码中包含多个 VisualStateManager.VisualStateGroups 时会导致界面无法正常显示,把这些代码注释掉就可以展示出界面. 
- 左连接,右连接,内连接,Union
			数据库的三种常用连接解析: 官方解释: 1.left [outer] join(左外联接) 返回 包括左表中的所有记录和右表中联结字段相等的记录 2.right [outer] join(右外联接) ... 
- JQuery鼠标移动上去显示预览图
			body中: <img src="../images/icon_view.gif" bigimg="../img.jpg" title="查看预 ... 
