当前位置:网站首页>leetcode 47. Permutations II full permutations II (medium)
leetcode 47. Permutations II full permutations II (medium)
2022-06-12 12:58:00 【InfoQ】
One 、 The main idea of the topic
- 1 <= nums.length <= 8
- -10 <= nums[i] <= 10
Two 、 Their thinking


3、 ... and 、 How to solve the problem
3.1 Java Realization
public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
// Construct a 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) {
// Use a two terminal queue
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();
}
}
}
}
Four 、 Summary notes
- 2022/6/12 To record the type of results, use a double ended queue
边栏推荐
- 下一个职场演讲PPT的明星,会不会是此刻的你【完美总结】
- Typescript and abstract classes
- [cloud native | kubernetes] kubernetes networkpolicy
- 关于派文的问题
- VTK three views
- R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting bar plot, adding significance level and jitter points
- 在 Debian 10 上独立安装MySQL数据库
- verilog-mode的简要介绍
- Robot Jacobian solution
- Bitmap, bloom filter and hash sharding
猜你喜欢
随机推荐
[EDA] chip layout design: VLSI layout design using electric
Embedded system hardware composition - embedded system hardware architecture
Uniapp wechat applet long press the identification QR code to jump to applet and personal wechat
One line of code to implement shell if else logic
Newton method for solving roots of polynomials
Jacobian matrix IK of manipulator
Buu question brushing record - 5
itk itk::BSplineDeformableTransform
R language ggplot2 visualization: use the ggrep package to add a number label to the data point at the end of the line plot
leetcode 47. Permutations II 全排列 II(中等)
Brush questions [de1ctf 2019]shellshellshell
Chrome debugging tool
Constant time delete / find any element in array
Binary tree (program)
【微信小程序开发】第1篇:开发工具安装及程序配置
The 4th Zhejiang CTF preliminary contest web pppop
itk itk::BSplineDeformableTransform
R语言ggplot2可视化:使用ggrepel包在线图(line plot)的尾端那个数据点添加数值标签(number label)
Theoretical knowledge of improved DH parameters and standard DH parameters of manipulator
Summary of question brushing in leetcode sliding window









![[database] Navicat -- Oracle database creation](/img/40/95d222acd0ae85bd9a4be66aa20d1d.png)