当前位置:网站首页>LeetCode_47_全排列Ⅱ
LeetCode_47_全排列Ⅱ
2022-07-23 07:02:00 【Fitz1318】
题目链接
题目描述
给定一个可包含重复数字的序列 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
解题思路
法一:回溯法+不剪枝
就是遍历所有可能,然后去重
AC代码
法一
class Solution {
boolean[] flag;
List<Integer> path = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
flag = new boolean[nums.length];
backTracing(nums);
return ans;
}
private void backTracing(int[] nums) {
if (path.size() == nums.length && !ans.contains(path)) {
ans.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (flag[i]) {
continue;
}
path.add(nums[i]);
flag[i] = true;
backTracing(nums);
path.remove(path.size() - 1);
flag[i] = false;
}
}
}
边栏推荐
- C#做一个简单浏览器
- prometheus+node-exporter+grafana 监控服务器系统资源
- Charles' bag grabbing tool test practice
- Typora 修改表格宽度
- Interface test - simple interface automation test demo
- 微服务重点
- GLIB-CRITICAL g_file_test:assertion ‘filename != null‘ failed
- 【JS高级】正则入门基础—关于你想知道的正则表达式_01
- 【 Visual Dispatching Software】 Shanghai Dow Ning apporte netronic download, Trial, tutoriel pour l'Organisation SMB
- Method of entering mathematical formula into mark down document
猜你喜欢

Charles抓包工具测试实战

【可视化调度软件】上海道宁为SMB组织带来NETRONIC下载、试用、教程
![Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断](/img/65/a735ca2c2902e3fc773dda79438972.png)
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断

回溯法解决 八皇后问题
![[图形学]ASTC纹理压缩格式](/img/d1/734fe91d56716c610980696a8e0d06.png)
[图形学]ASTC纹理压缩格式

QT creator.Pro file adds the corresponding library according to the kit

解决MySQL向表中增加数据插入中文乱码问题
Smart canteen data analysis system
![[visual scheduling software] Shanghai daoning brings netronic downloads, trials and tutorials to SMB organizations](/img/2f/ea0d6ceefca84ef4aeef9c384861f9.png)
[visual scheduling software] Shanghai daoning brings netronic downloads, trials and tutorials to SMB organizations

Smart city infrastructure management based on bim+3dgis
随机推荐
关于#redis#的问题:Redis设置数据持久化之后还是会有丢失数据的风险
Introduction to radar part vii 1 radar and resolution
About this pointer
高中语文教资考试大纲
Shell operator, $((expression)) "or" $[expression], expr method, condition judgment, test condition, [condition], comparison between two integers, judgment according to file permission, judgment accor
PHP gets the current timestamp three bit MS MS timestamp
The principle of Google interview questions is to analyze 12 table tennis balls, one of which is defective. Weigh it with a balance for 3 times to find it
在虚拟环境下使用pip时默认使用系统环境的pip该怎么办
基于BIM+3DGIS的智慧城市基础设施管理
Client does not support authentication protocol requested by server; consider upgrading MySQL client
高性能JVM的参数
关于this指针
数据库系统原理与应用教程(048)—— MySQL 查询(十):自连接查询
MySQL面试题
How to deal with the new development mode when doing testing?
vs2019:constexpr 函数“qCountLeadingZeroBits”不能生成常量表达式
数据库系统原理与应用教程(041)—— MySQL 查询(三):设置查询条件
4D antenna array layout design
-XX:+UseCGroupMemoryLimitForHeap 无法创建虚拟机问题
深入解读 EVM 的生态帝国