47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[[1,1,2],[1,2,1],[2,1,1]
]
难度:medium
题目:给定一组可能包含重复数的集合,返回所有可能的排列。
思路:先对数组进行排序,为去重做准备。然后借助递归遍历所有数。重点在于,当每次选定一个数做完交换之后,恢复时要对两个交换数及之间的所有数做排序。继续为接下来的去重做准备。
class Solution {public List<List<Integer>> permuteUnique(int[] nums) {Arrays.sort(nums);List<List<Integer>> result = new ArrayList<>();permuteUnique(nums, 0, new Stack<Integer>(), result);return result;}private void permuteUnique(int[] nums, int idx, Stack<Integer> stack, List<List<Integer>> result) {if (idx == nums.length) {result.add(new ArrayList<>(stack));return;}for (int i = idx; i < nums.length; i++) {if (idx == i || nums[i] != nums[i - 1]) {stack.push(nums[i]);swap(nums, idx, i);permuteUnique(nums, idx + 1, stack, result);swap(nums, i, idx);Arrays.sort(nums, idx, i + 1);stack.pop();}}}private void swap(int[] nums, int i, int j) {int t = nums[i];nums[i] = nums[j];nums[j] = t;}
}
47. Permutations II相关推荐
- 【数字全排列】LeetCode 47. Permutations II
LeetCode 47. Permutations II Solution1:我的答案 笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~ class Solution { p ...
- 47. Permutations II 1
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- LeetCode 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] ...
- [LeetCode]47. 全排列 II
47. 全排列 II 难度中等761收藏分享切换为英文接收动态反馈 给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列. 示例 1: 输入:nums = [1,1,2] 输出 ...
- LeetCode 47. 全排列 II【数组,回溯算法,排序去重】
47. 全排列 II 给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列. 示例 1: 输入:nums = [1,1,2] 输出: [[1,1,2], [1,2,1], [2 ...
- 46. 全排列 47. 全排列 II
46. 全排列 给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 .你可以 按任意顺序 返回答案. 示例 1: 输入:nums = [1,2,3] 输出:[[1,2,3],[1,3,2 ...
- Leetcode: Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
最新文章
- 没有导师指导,该如何自己选题发CVPR?
- 1 Knight Moves
- matlab中bitget函数用法_MATLAB中SVM的用法
- 服务器日志清理及IIS日志的清理
- Kubernetes Service(溪恒)
- 北斗导航 | GPS卫星导航技术重要人物简介
- 如何改变请求的host以及referer抓取做了host以及referer限制的接口数据
- 线程Thread类的start()方法和run()方法
- 关于C#关闭窗体后,依旧有后台进程在运行的解决方法
- PHP:车牌照合法性
- 也谈谈让好马吃回头草
- matlab arccos uint8,《高等应用数学问题的MATLAB求解》——第3章习题代码
- step7设置pcpg_安装STEP7编程软件过程及PG/PC接口设置
- 最新版万能编辑器Visual Studio Code安装使用教程
- 1362:家庭问题(family)(并查集)
- 自然语言处理是什么,我们为什么需要处理自然语言?
- python多进程假死
- 易基因 | 植物DNA甲基化专题 | NAR:拟南芥AtHDA6与着丝粒周围DNA甲基化关系研究
- 理解无线电波极化与天线极化
- matlab中的高阶导数,MATLAB如何求函数的n阶导数?
热门文章
- javaweb学习总结二十五(response对象的用法一)
- 离开页面前显示确认提示对话框(兼容IE,firefox) = how to Catch Win...
- VIM的snipMate的继承设置
- GNU make manual 翻译( 一百二十一)
- Windows服务初探
- React中如何优雅的捕捉事件错误
- Android图片三级缓存策略
- 前端面试常考知识点---js
- Python练习题(day1)
- SQL Server-聚焦NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL性能分析(十八)