当前位置:网站首页>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);
}
边栏推荐
- Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
- Hardware Basics - diode Basics
- IP lab monthly resumption · issue 5
- Excel quickly merges multiple rows of data
- Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
- 吃透Chisel语言.04.Chisel基础(一)——信号类型和常量
- go vendor 项目迁移到 mod 项目
- PHP log debugging
- CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)
- sql优化之查询优化器
猜你喜欢
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
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
软件测试之测试评估
Qt如何实现打包,实现EXE分享
Mask wearing detection based on yolov1
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
sql优化之查询优化器
【信息检索】链接分析
Data warehouse interview question preparation
随机推荐
DDD application and practice of domestic hotel transactions -- Code
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
TestSuite and testrunner in unittest
游戏出海,全球化运营
Deming Lee listed on Shenzhen Stock Exchange: the market value is 3.1 billion, which is the husband and wife of Li Hu and Tian Hua
LifeCycle
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
Can mortgage with housing exclude compulsory execution
1200. Minimum absolute difference
测试流程整理(3)
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
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
安装Mysql
【信息检索】链接分析
【信息检索】分类和聚类的实验
Install and use MAC redis, connect to remote server redis
Introducing testfixture into unittest framework
2022 game going to sea practical release strategy
The font of markdown grammar is marked in red