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 ...
随机推荐
- POJ2449:K短路
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 26355 Accepted: 7170 ...
- UDK编辑器 49条小提示
转自:http://www.cnblogs.com/hmxp8/archive/2012/02/09/2343674.html Very Helpful~ 01. First time using t ...
- angular-cli.json配置参数解析,常用命令解析
1.angular-cli.json配置参数解析 { "project": { "name": "ng-admin", //项目名称 &qu ...
- 7、RNAseq Downstream Analysis
Created by Dennis C Wylie, last modified on Jun 29, 2015 Machine learning methods (including cluster ...
- 1.3 DVWA亲测sql注入漏洞
LOW等级 我们先输入1 我们加上一个单引号,页面报错 我们看一下源代码: <?php if( isset( $_REQUEST[ 'Submit' ] ) ) { // Get input ...
- Linux常用知识点汇总
常用命令 1.ls 列出目录下的所有文件及文件夹 2.pwd 打印出当前所在目录 3. ./ 执行 .sh 文件命令 4.ip addr 查看ip地址 5.sudo service network ...
- Sharepoint foundation2013独立安装教程
Sharepoint foundation2013安装教程 一,什么是sharepoint foundation2013 大家都知道Sharepoint是一个非常强大的企业级开发平台,它包含各种功能比 ...
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- Boost Python官方样例(二)
返回值 使用return_by_value有点像C++ 11的auto关键字,可以让模板自适应返回值类型(返回值类型必须是要拷贝到新的python对象的任意引用或值类型),可以使用return_by_ ...
- 【leetcode 3. 无重复字符的最长子串】解题报告
思路:滑动窗口的思想 方法一:滑动窗口 int lengthOfLongestSubstring(string s) { /* 控制一个滑动窗口,窗口内的字符都是不重复的,通过set可以做到判断字符是 ...