当前位置:网站首页>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;
}
}
}
边栏推荐
- Ros2 self study notes: gazebo physical simulation platform
- [Muduo] epollplayer event distributor
- Ti single chip millimeter wave radar code walk (XXV) -- angular dimension (3D) processing flow
- 【STM32】串口通信基础知识
- Database view detailed exploration
- Machine learning, Wu Enda, logical regression
- LeetCode_2341_数组能形成多少数对
- 【cocos creator】spine动画,监听播放结束
- 解决MySQL向表中增加数据插入中文乱码问题
- Learn to use canvas to build line chart, bar chart and pie chart
猜你喜欢

keepalived双机热备

Research on hardware architecture of Ti single chip millimeter wave radar xwr1642

Talking about the CPU type of anroid device and the placement directory of so files

Interface test - simple interface automation test demo

Why choose AMD epyc for cloud solutions?

Shell operator, $((expression)) "or" $[expression], expr method, condition judgment, test condition, [condition], comparison between two integers, judgment according to file permission, judgment accor
![[图形学]ASTC纹理压缩格式](/img/d1/734fe91d56716c610980696a8e0d06.png)
[图形学]ASTC纹理压缩格式

Introduction to radar part vii 2 imaging method

【深入浅出玩转FPGA学习10------简单的Testbench设计】

Point target simulation of SAR imaging (II) -- matlab simulation
随机推荐
[visual scheduling software] Shanghai daoning brings netronic downloads, trials and tutorials to SMB organizations
Problem solving: script file 'scripts\pip script py‘ is not present.
vs2019:constexpr 函数“qCountLeadingZeroBits”不能生成常量表达式
Typora modify table width
数据库系统原理与应用教程(047)—— MySQL 查询(九):连接查询
SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
面试官:有了解过ReentrantLock的底层实现吗?说说看
Machine learning, Wu Enda, logical regression
Vs2019:constexpr function "qcountleadingzerobits" cannot generate constant expressions
数据库系统原理与应用教程(039)—— MySQL 查询(一):SELECT 命令的语法分析
专题讲座5 组合数学 学习心得(长期更新)
In depth interpretation of EVM's ecological Empire
回溯法解决 八皇后问题
Deep understanding of the underlying framework of wechat applet (I)
ES6——周考题
同花顺开户风险性大吗,安全吗?
轻重链剖分/树链剖分
-XX:+UseCGroupMemoryLimitForHeap 无法创建虚拟机问题
Ti single chip millimeter wave radar code walk (XXV) -- angular dimension (3D) processing flow
MySQL index transaction & JDBC programming