Permutations II
来源:互联网 发布:smg网络意思 编辑:程序博客网 时间:2024/04/29 10:10
题目
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]
, and[2,1,1]
.
方法
和上一题的方法一样,利用nextPermutation的思想。http://blog.csdn.net/u010378705/article/details/31348875private boolean isReverseOrder(int[] num) {for (int i = 0; i < num.length - 1; i++) {if (num[i] < num[i + 1]) {return false;}}return true;} public List<List<Integer>> permuteUnique(int[] num) { List<List<Integer>> list = new ArrayList<List<Integer>>(); if (num == null || num.length == 0) { return list; } Arrays.sort(num); while (!isReverseOrder(num)) { List<Integer> subList = new ArrayList<Integer>(); for (int i = 0; i < num.length; i++) { subList.add(num[i]); } list.add(subList); nextPermutation(num); } List<Integer> subList = new ArrayList<Integer>();for (int i = 0; i < num.length; i++) {subList.add(num[i]);}list.add(subList);nextPermutation(num); return list; } public void nextPermutation(int[] num) { if (num != null && num.length != 0 && num.length != 1) { int len = num.length; int i = len; for (; i > 1; i--) { if (num[i - 1] > num[i - 2]) { int flag = 0; for (int k = i - 1; k < len; k++) { if (num[k] <= num[i - 2]) { flag = k - 1; break; } } if (flag == 0) { flag = len - 1; } int temp = num[flag]; num[flag] = num[i - 2]; num[i - 2] = temp; Arrays.sort(num, i - 1, len); break; } } } }
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Permutations and Permutations II
- Permutations && Permutations ii
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- 深入指针
- 黑马程序员——枚举类
- 机器学习
- 广义表
- 官方文档 恢复备份指南三 Recovery Manager Architecture
- Permutations II
- Linux的组成
- LED摇动显示棒
- 直接插入排序
- 【OJ】---L---对象数组输入与输出
- 关于递归
- conn.setAutoCommit()用法及作用(转发)
- 硬件工程师 基础知识 架构
- char **, char *a[] ,char (*a)[] 思考