[leetcode] 47. Permutations II
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]
] 和一般的Permutation不一样的是,这种permutation需要排序,使相同的元素能够相邻,选取下一个元素的时候,要查看这个元素的前一个元素是否和它相同,如果相同而且没有使用过,就不用选取这个元素,因为如果选取了这个元素,所得的结果被包含于选取了前一个相同元素的结果中。
代码如下:
public class Solution {
int length;
public List<List<Integer>> permuteUnique(int[] nums) {
length = nums.length;
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (length == 0) {
return result;
}
Arrays.sort(nums);
boolean[] flags = new boolean[length];
Arrays.fill(flags, false);
List<Integer> candidate = new ArrayList<Integer>();
helper(nums, length, flags, result, candidate);
return result;
}
private void helper(int[] nums, int n, boolean[] flags, List<List<Integer>> result, List<Integer> candidate) {
if (n == 0) {
result.add(new ArrayList<Integer>(candidate));
}
int ll = candidate.size();
for (int i = 0; i < length; i++) {
if (!flags[i]) {
//if the number appears more than once and the same number before it has not been used
if (i != 0 && nums[i] == nums[i - 1] && !flags[i - 1]) {
continue;
}
flags[i] = true;
candidate.add(nums[i]);
helper(nums, n - 1, flags, result, candidate);
candidate.remove(ll);
flags[i] = false;
}
}
}
}
[leetcode] 47. Permutations II的更多相关文章
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列 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 ...
- LeetCode 47 Permutations II(全排列)
题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] 47. 全排列 II
题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- 【BZOJ-3195】奇怪的道路 状压DP (好题!)
3195: [Jxoi2012]奇怪的道路 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: 184[Submit][Statu ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 优化Table View
优化Table View Table view需要有很好的滚动性能,不然用户会在滚动过程中发现动画的瑕疵. 为了保证table view平滑滚动,确保你采取了以下的措施: 正确使用`reuseIden ...
- Character类
Character类 用来判断大小写 方法: public static boolean isUpperCase(char ch):判断是否大写 public static boolean isLow ...
- [转] Unity Mathf 数学运算(C#)
Mathf.Abs 绝对值 计算并返回指定参数 f 绝对值. Mathf.Acos 反余弦 static function Acos (f : float) : float 以弧度为单位计算并返回参数 ...
- 2015.4.21 实现一般免登陆,微博QQ分享,字体自适应等
1.实现一般的登录验证和免登陆: 解决方法:node方法代码,nodeJS实现的session模块,不完整,但能用,仅供参考. 语言无所谓,session的机制都是一样的,实现不一样而已,: 2. ...
- 创立一个网站的前前后后(起因,域名,云平台,备案,CDN等等)(1)
起因 写完<完美软件开发:方法与逻辑>这书后,原本想继续写书的,可出来参加了些社区活动后,我发现我写的书大家评价还行,但其实不太理解.而接下来想写的书更加抽象点,准备叫<管理的解析& ...
- LNMP环境搭建笔记
说明:前面尝试的在ubuntu12.04上搭建的LAMP环境由于开发的需要需要对php的版本进行升级,然而通过apt-get库安装的php的版本是5.3.10,不能满足开发需要.此笔记安装的php的 ...
- There are no resources that can be added or removed from the server
第1步.新建一个“Dynamic Web Project” 第2步.把新建项目里面的.project文件和.settings文件夹复制到导入的那个项目里面. 第3步.把web projiect set ...
- 《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及 如何使用外部共享库和头文件
<CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...