当前位置:网站首页>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);
}
边栏推荐
- The game goes to sea and operates globally
- 2022游戏出海实用发行策略
- 吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件
- [R language data science]: cross validation and looking back
- Test process arrangement (3)
- sql优化之查询优化器
- IP lab monthly resumption · issue 5
- Assertion of unittest framework
- Gorm data insertion (transfer)
- Introducing testfixture into unittest framework
猜你喜欢
随机推荐
游戏出海,全球化运营
WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
LifeCycle
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
自主工业软件的创新与发展
Test process arrangement (3)
数据仓库面试问题准备
测试流程整理(3)
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
Install and use MAC redis, connect to remote server redis
Test evaluation of software testing
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
Ruiji takeout notes
Incremental ternary subsequence [greedy training]
Assertion of unittest framework
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses the by parameter to specify the groupin
Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering
Data warehouse interview question preparation









