Permutations II ——LeetCode
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].
题目大意:给一个数组,包含重复元素,返回所有可能组合,去重。
解题思路:这题是Permutation的变种,思路就是回溯。首先把数组排序一下,对于不是第一次使用的数字,检测是否出现过。
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
Arrays.sort(nums);
List<Integer> tmp = new ArrayList<>();
boolean[] used = new boolean[nums.length];
helper(res, tmp, 0, nums.length, nums, used);
return res;
}
public void helper(List<List<Integer>> res, List<Integer> tmp, int n, int N, int[] nums, boolean[] used) {
if (n == N) {
res.add(new ArrayList<>(tmp));
return;
}
int last_num = 0x80000000;
for (int i = 0; i < N; i++) {
if (!used[i] && last_num != nums[i]) {
used[i] = true;
tmp.add(nums[i]);
last_num = nums[i];
helper(res, tmp, n + 1, N, nums, used);
used[i] = false;
tmp.remove(tmp.size() - 1);
}
}
}
Permutations II ——LeetCode的更多相关文章
- Permutations II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...
- Permutations II leetcode java
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
随机推荐
- 遍历id,根据id作为条件循环查询。
string id = "OE09924008161405102,OE36765709071405102,OE87852044171405102,OE09924008161405102&qu ...
- (转)基于PHP的cURL快速入门
1. 原文:基于PHP的cURL快速入门 英文原文:http://net.tutsplus.com/tutorial ... for-mastering-curl/ 原文作者:Burak Guzel ...
- Android开发系列----sdk下载 环境准备
今天开始准备Android开发环境,FQ下载Android Studio,官网下载地址 https://developer.android.com/studio/install.html (突然发现我 ...
- 运用BeanUtils构建通用的查询 更新方法(个人拙作,不喜勿喷)
------------------------------------更新方法----------------------------------- public void update(Strin ...
- Linux的压缩解压命令快速上手——解压篇
在Linux系统中,压缩文件通常是先将若干文件(包括目录)打包成一个tar文件,然后再调用压缩程序将tar文件压缩成相应的压缩包,这也就是为什么Linux系的压缩包的后缀通常都是像tar.gz,tar ...
- shell调用shell
在默认条件下,执行shell文件会出现permission denied报错,一般是没有可执行权限.用chmod修改权限 chomd 777 score.sh //把所有权限给aa文件 777代表 ...
- java 全角、半角字符串转换
转自:http://www.cnblogs.com/modou/articles/2679815.html 加入了空字符串的验证 半角转全角的方法: /** * @Title: ToSBC * ...
- Bootstrap_Javascript_按钮插件
一 . 加载状态按钮 HTML: <button class="btnbtn-primary" data-loading-text="正在加载中,请稍等...&qu ...
- TatukGIS - GisDefs - CanonicalSQLName 函数
函数名称 CanonicalSQLName 所在单元 GisDefs 函数原型 function CanonicalSQLName(const _name: String; const _tem ...
- centos下Elasticsearch数据迁移与备份
########### ### 共享创建es官方网站就一句话 ######## 1.下载 文件共享 .. rpm -i http://mirror.symnds.com/distributions ...