47. Permutations II
来源:互联网 发布:无锡移动网络办理 编辑:程序博客网 时间:2024/06/08 11:23
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]]求排列组合,去掉重复的。程序如下:
class Solution { public void swap(int[] nums, int i, int j){ int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } public void traceBacking(int[] nums, List<List<Integer>> llst, int start){ if (start == nums.length){ List<Integer> lst = new ArrayList<>(); for (int val : nums){ lst.add(val); } llst.add(lst); return; } for (int i = start; i < nums.length; ++ i){ if (i != start&&(nums[start] == nums[i]||nums[i-1] == nums[i])){ continue; } swap(nums, start, i); traceBacking(nums, llst, start + 1); // swap(nums, start, i); } for (int i = start; i < nums.length - 1; ++ i){ swap(nums, i+1, i); } } public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(nums); traceBacking(nums, result, 0); return result; }}
阅读全文
0 0
- 46. Permutations && 47. Permutations II
- 46. Permutations, 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
- 47. Permutations II
- LeetCode 47. Permutations II
- 47. Permutations II
- 47. Permutations II LeetCode
- 47. Permutations II
- 47. Permutations II
- 47. Permutations II
- 47. Permutations II
- Leetcode 47. Permutations II
- 47. Permutations II
- c++基础之define宏定义
- LeanCloud save object fail
- sdnu1138shopping
- sRGB色域与NTSC色域
- ESP32-使用eFuse 固化VDD_SDIO支持电压,释放GPIO12
- 47. Permutations II
- 通过序列和触发器实现Oracle主键自增长
- Java的方法重载规则
- 关于两个常用网络命令原理和一点说明
- Hibernate 注解映射使用方式案例
- Nginx主配置参数详解,Nginx配置网站
- 用户 'IIS APPPOOL\***' 登录失败的原因及两种解决办法
- rpm介绍
- 剑指offer:字符流中第一个不重复的字符