Java实现 LeetCode 47 全排列 II(二)
47. 全排列 II
给定一个可包含重复数字的序列,返回所有不重复的全排列。
示例:
输入: [1,1,2]
输出:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
class Solution {
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
dfs(nums,0);
return ans;
}
private void dfs(int[] nums,int cur) {
if (cur == nums.length) {
List<Integer> line = new ArrayList<>();
for (int i : nums) {
line.add(i);
}
ans.add(line);
} else {
for (int i = cur;i < nums.length;i++) {
if (canSwap(nums,cur,i)) {
swap(nums,cur,i);
dfs(nums,cur + 1);
swap(nums,cur,i);
}
}
}
}
private boolean canSwap(int nums[],int begin,int end) {
for (int i = begin;i < end;i++) {
if (nums[i] == nums[end]) {
return false;
}
}
return true;
}
private void swap(int nums[],int i,int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
Java实现 LeetCode 47 全排列 II(二)的更多相关文章
- [LeetCode] 47. 全排列 II
题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...
- LeetCode 47 全排列II
题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 与上一题相比,这题多了一 ...
- LeetCode 47. 全排列 II(Permutations II)
题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
- leetcode 46. 全排列 及 47. 全排列 II
46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...
- 47. 全排列 II
47. 全排列 II 题意 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]] 解题思路 去重的全排列 ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- pthon-安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示“This application failed to start because no Qt platform plugin could be initialized.Reinstalling the application the application may fix this program”
最近学习python,安装网上教程一步一步的安装,网上很多帖子都写的非常详细,不由深深感慨多谢各位不辞辛苦的记录,指导着来自新入门的同学. 但是实际安装中,最理想莫过于一次性安装成功,但自己安装就出现 ...
- Apache Poi实现excel解析
一.说明 1.本文通过使用 poi 工具解析 excel 表格数据,实现导入导出 2.excel目前有两种格式 2003版本的 excel.xls 与 2007版本的 excel.xlsx ,注意两种 ...
- python--制作微信好友照片墙
知识来源:https://zhuanlan.zhihu.com/p/73975013 1.环境 os:MAC tool:python 3.7 ,pip3.7 2.前提: 使用pip3.7 instal ...
- UEFI Shell --常用命令解释
UEFI Shell解释 UEFI Shell 是一个提供用户和UEFI系统之间的接口,进入UEFI Shell可以对计算机系统进行配置 命令解释: 单独的help就可以输出所有指令,不做特殊说明,内 ...
- Hadoop CDH版本安装和启动(CentOS7)
1.创建hadoop组和用户,useradd hadoop passwd hadoop groupadd hadoops usermod -G hadoops hadoop(将hadoop添加到had ...
- 手把手numpy教程【二】——数组与切片
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Numpy专题的第二篇,我们来进入正题,来看看Numpy的运算. 上一篇文章当中曾经提到过,同样大小的数据,使用Numpy的运算速度会 ...
- MySQL zip解压 安装过程和配置
MYSQL官网下载地址:https://dev.mysql.com/downloads/mysql/ 1.下载mysql-5.7.19-winx64.zip,解压到指定的文件夹, 例如:E:\so ...
- linux常用命令---用户相关操作
用户相关操作
- mysql运维入门3:MyISAM和InnoDB
myisam 5.1的默认存储类型 基于传统的ISAM类型,Indexed Sequential Access Method,有索引的顺序访问方法 存储记录文件的标准方法 不是事务安全,不支持外键 表 ...
- jQuery中效果animate方法解决width是百分比出现的问题
jQuery中效果animate方法解决width是百分比出现的问题 http://www.mafutian.net/131.html 问题描述: 效果如图,初始化,每个层宽20%,采用animate ...