leetcode 47. Permutations II
来源:互联网 发布:51排课软件 编辑:程序博客网 时间:2024/06/10 01:05
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:
[ [1,1,2], [1,2,1], [2,1,1]]
还是全排列问题,这次有重复数字,是一个通用解法,每次递归要看该数字是否是重复数字,如果是的话,前一个数字有没有被使用,使用了则可以递归,没有使用的话,该数字也要跳过。
public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if(nums.length==0){ return res; } Arrays.sort(nums); List<Integer> item = new ArrayList<Integer>(); boolean []use = new boolean[nums.length]; helper(nums,res,item,use); return res; } public void helper(int[]nums,List<List<Integer>>res,List<Integer>item,boolean[]use){ if(item.size()==nums.length){ res.add(new ArrayList<Integer>(item)); return ; } for(int i=0;i<nums.length;i++){ if(i>0&&!use[i-1]&&nums[i-1]==nums[i])continue; if(!use[i]){ use[i]=true; item.add(nums[i]); helper(nums,res,item,use); item.remove(item.size()-1); use[i]=false; } } }}
阅读全文
0 0
- [LeetCode]47.Permutations II
- LeetCode 47.Permutations II
- LeetCode --- 47. Permutations II
- [Leetcode] 47. Permutations II
- [leetcode] 47.Permutations II
- LeetCode 47. Permutations II
- 47. Permutations II LeetCode
- Leetcode 47. Permutations II
- LeetCode 47. Permutations II
- LeetCode - 47. Permutations II
- [LeetCode] 47. Permutations II
- leetcode 47. Permutations II
- [LeetCode]--47. Permutations II
- [LeetCode]--47. Permutations II
- [leetcode] 47. Permutations II
- leetcode 47. Permutations II
- Leetcode-47. Permutations II
- [LeetCode]--47. Permutations II
- 实训第一天笔记
- SSM整合之环境配置和测试开发
- 最简洁代码实现Listview多选
- 欢迎使用CSDN-markdown编辑器
- 使用JavaScript实现回到顶部效果
- leetcode 47. Permutations II
- iOS 扫描二维码(ZBarSDK)遇到的坑~解决方法
- RocketMQ4.0源码分析之-路由管理
- 通过api获取句柄控制其他窗体
- nonatomic, retain,weak,strong用法详解
- 今天,我拥有了自己的博客
- 什么是web应用程序
- 想要开发自己的PHP框架需要那些知识储备?
- Redis 精品图书和文章介绍