当前位置:网站首页>leetcode 47. Permutations II 全排列 II(中等)
leetcode 47. Permutations II 全排列 II(中等)
2022-06-12 12:42:00 【InfoQ】
一、题目大意
- 1 <= nums.length <= 8
- -10 <= nums[i] <= 10
二、解题思路


三、解题方法
3.1 Java实现
public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
// 构造一个hashmap
Map<Integer, Integer> countMap = new HashMap<>();
for (int n : nums) {
int count = countMap.getOrDefault(n, 0);
countMap.put(n, count + 1);
}
dfs(countMap, nums.length, new LinkedList<>(), ans);
return ans;
}
void dfs(Map<Integer, Integer> countMap, int total, Deque<Integer> perm, List<List<Integer>> ans) {
// 使用双端队列
if (perm.size() == total) {
ans.add(new ArrayList<>(perm));
}
for (Map.Entry<Integer, Integer> tmp : countMap.entrySet()) {
if (tmp.getValue() > 0) {
int oldValue = tmp.getValue();
perm.offerFirst(tmp.getKey());
tmp.setValue(tmp.getValue() - 1);
dfs(countMap, total, perm, ans);
tmp.setValue(oldValue);
perm.pollFirst();
}
}
}
}
四、总结小记
- 2022/6/12 来记录结果的类型要用双端队列
边栏推荐
- El select data echo, display only value but not label
- [HXBCTF 2021]easywill
- Time series database - incluxdb2 docker installation
- Eight misunderstandings are broken one by one (2): poor performance? Fewer applications? You worry a lot about the cloud!
- 二叉树(思路篇)
- 嵌入式驱动程序设计
- 提升管道效率:你需要知道如何识别CI/CD管道中的主要障碍
- 【VIM】.vimrc配置,已经安装Vundle,YoucompleteMe
- 关系代数笛卡尔积和自然连接的例子
- Object value taking method in JS And []
猜你喜欢
![[you code, I fix] whitesource was officially renamed mend](/img/0f/75ac1d0b08244ed689733d38551a61.jpg)
[you code, I fix] whitesource was officially renamed mend

实战 | 巧用位姿解算实现单目相机测距

Advanced C language -- storage of floating point in memory

Geek challenge 2021 Web

Promise+ handwritten promise

安全KNN

Advanced C language -- storage of deep anatomical data in memory (with exercise)

Microsoft Word tutorial, how to insert a header or footer in word?

C语言深度解剖篇——关键字&&补充内容

The 4th Zhejiang CTF preliminary contest web pppop
随机推荐
2021-11-16
Five ways to quickly download large files from Google cloud disk
Advanced C language -- storage of floating point in memory
2022 ARTS|Week 23
一行代码实现shell if else逻辑
Buu question brushing record - 6
路由信息的来源
Detect whether the vector has an intersection
VGA显示彩条和图片(FPGA)
嵌入式系统概述1-嵌入式系统定义、特点和发展历程
机械臂改进的DH参数与标准DH参数理论知识
快速下载谷歌云盘大文件的5种方法
功能标记是什么?一文了解它的作用,以及它的最佳实践
嵌入式系统硬件构成-嵌入式系统硬件体系结构
Native JS implements the copy text function
Newton method for solving roots of polynomials
Object. Detailed explanation of assign()
unittest框架
442个作者100页论文!谷歌耗时2年发布大模型新基准BIG-Bench | 开源
【您编码,我修复】WhiteSource正式更名为Mend