当前位置:网站首页>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);
}
边栏推荐
- How to package QT and share exe
- Unity shader learning (3) try to draw a circle
- vscode 常用插件汇总
- Excel quickly merges multiple rows of data
- ViewModel 初体验
- Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
- Product identification of intelligent retail cabinet based on paddlex
- go vendor 项目迁移到 mod 项目
- MySQL 5 installation and modification free
- 去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
猜你喜欢
MySQL 5 installation and modification free
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
Xcode 异常图片导致ipa包增大问题
基于PaddleX的智能零售柜商品识别
Unity Shader学习(三)试着绘制一个圆
sql优化之查询优化器
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
Ruiji takeout notes
随机推荐
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
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
Common content type correspondence table
Mask wearing detection based on yolov1
C# wpf 实现截屏框实时截屏功能
C # WPF realizes the real-time screen capture function of screen capture box
DDD application and practice of domestic hotel transactions -- Code
Why should Base64 encoding be used for image transmission
吃透Chisel语言.04.Chisel基础(一)——信号类型和常量
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
Data warehouse interview question preparation
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
海外游戏代投需要注意的
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
瑞吉外卖笔记
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test