当前位置:网站首页>Leetcode T47: 全排列II
Leetcode T47: 全排列II
2022-07-04 12:51:00 【范谦之】
题目描述
给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
示例 1:
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]
示例 2:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
提示:
- 1 <= nums.length <= 8
- -10 <= nums[i] <= 10
思路
利用HashMap去重
代码
int n;
int[] a;
Map<List<Integer>, Integer> res;
void prem(int cur) {
if(cur == n) {
List<Integer> lis = new ArrayList<Integer>();
for(int x: a) lis.add(x);
res.put(lis, 1);
} else {
for(int i = cur; i < n; i++) {
int t = a[i]; a[i] = a[cur]; a[cur] = t;
prem(cur+1);
t = a[i]; a[i] = a[cur]; a[cur] = t;
}
}
}
public List<List<Integer>> permuteUnique(int[] nums) {
a = nums;
n = nums.length;
res = new HashMap<List<Integer>, Integer>();
prem(0);
return new ArrayList<List<Integer>>(res.keySet());
}
void test() throws IOException {
Reader cin = new Reader();
int[] a = {
1,2,3};
permuteUnique(a);
}
边栏推荐
- Idea shortcut keys
- Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
- Install MySQL
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
- R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
- Basic mode of service mesh
- R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
- Rich text editing: wangeditor tutorial
- R language ggplot2 visualization: gganimate package creates animated graph (GIF) and uses anim_ The save function saves the GIF visual animation
- Use the default route as the route to the Internet
猜你喜欢

数据仓库面试问题准备

Vscode common plug-ins summary

TestSuite and testrunner in unittest

基于YOLOv1的口罩佩戴检测

【信息检索】分类和聚类的实验

按照功能对Boost库进行分类

吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest

Excel快速合并多行数据

Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test

【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
随机推荐
Idea shortcut keys
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
Use of tiledlayout function in MATLAB
卷积神经网络经典论文集合(深度学习分类篇)
Unittest中的TestSuite和TestRunner
Migration from go vendor project to mod project
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
sql优化之explain
R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
测试流程整理(2)
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
基于PaddleX的智能零售柜商品识别
mac redis安装与使用,连接远程服务器 redis
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
Learn kernel 3: use GDB to track the kernel call chain
基于YOLOv1的口罩佩戴检测
sharding key type not supported
WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
The game goes to sea and operates globally