当前位置:网站首页>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);
}
边栏推荐
- 学内核之三:使用GDB跟踪内核调用链
- Basic mode of service mesh
- Error in find command: paths must precede expression (turn)
- 游戏出海,全球化运营
- R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
- 为什么图片传输要使用base64编码
- Unittest中的TestSuite和TestRunner
- 吃透Chisel语言.03.写给Verilog转Chisel的开发者(没有Verilog基础也可以看看)
- 吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程
- Data warehouse interview question preparation
猜你喜欢

The font of markdown grammar is marked in red

Test process arrangement (2)

10.(地图数据篇)离线地形数据处理(供Cesium使用)

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

Test process arrangement (3)

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)
![Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]

Install MySQL

【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法

2022 practice questions and mock exams for the main principals of hazardous chemical business units
随机推荐
LiveData
Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
Huahao Zhongtian rushes to the scientific and Technological Innovation Board: the annual loss is 280million, and it is proposed to raise 1.5 billion. Beida pharmaceutical is a shareholder
Vscode common plug-ins summary
R语言使用dplyr包的group_by函数和summarise函数基于分组变量计算目标变量的均值、标准差
IP 实验室月复盘 · 第 5 期
游戏出海,全球化运营
sharding key type not supported
Qt如何实现打包,实现EXE分享
自主工业软件的创新与发展
C# wpf 实现截屏框实时截屏功能
qt 怎么检测鼠标在不在某个控件上
Use of tiledlayout function in MATLAB
基于51单片机的超声波测距仪
1200. Minimum absolute difference
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
按照功能对Boost库进行分类
392. Judgement subsequence
File creation, writing, reading, deletion (transfer) in go language