LeetCode - 47. Permutations II
2016-06-29 12:52
316 查看
采用46. Permutation中的第二种字典序的方法的话,会自动去除重复的list,所以可以直接把46的解法二直接拿过来,代码如下:
public class Solution{
List<List<Integer>> result = new ArrayList<List<Integer>>();
public int factorial(int x){
return (x == 0) || (x == 1) ? 1 : factorial(x - 1) * x;
}
public void nextPermutation(int[] nums){
int i = nums.length - 1;
while(i > 0 && nums[i] <= nums[i - 1]){
i--;
}
if(i <= 0) return;
int j = nums.length - 1;
while(j >= i && nums[j] <= nums[i - 1]){
j--;
}
int temp = nums[i - 1];
nums[i - 1] = nums[j];
nums[j] = temp;
int l = i;
int r = nums.length - 1;
while(l < r){
temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
List<Integer> list = new ArrayList<Integer>();
for(int x : nums){
list.add(x);
}
result.add(list);
}
public List<List<Integer>> permuteUnique(int[] nums){
// Corner case
if(nums == null || nums.length == 0){
return result;
}
// Sort nums
Arrays.sort(nums);
// Convert nums to list
List<Integer> list = new ArrayList<Integer>();
for(int x : nums){
list.add(x);
}
result.add(list);
// Add next permutation iterativel
for(int i = 1; i < factorial(nums.length); i++){
nextPermutation(nums);
}
return result;
}
}
public class Solution{
List<List<Integer>> result = new ArrayList<List<Integer>>();
public int factorial(int x){
return (x == 0) || (x == 1) ? 1 : factorial(x - 1) * x;
}
public void nextPermutation(int[] nums){
int i = nums.length - 1;
while(i > 0 && nums[i] <= nums[i - 1]){
i--;
}
if(i <= 0) return;
int j = nums.length - 1;
while(j >= i && nums[j] <= nums[i - 1]){
j--;
}
int temp = nums[i - 1];
nums[i - 1] = nums[j];
nums[j] = temp;
int l = i;
int r = nums.length - 1;
while(l < r){
temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
List<Integer> list = new ArrayList<Integer>();
for(int x : nums){
list.add(x);
}
result.add(list);
}
public List<List<Integer>> permuteUnique(int[] nums){
// Corner case
if(nums == null || nums.length == 0){
return result;
}
// Sort nums
Arrays.sort(nums);
// Convert nums to list
List<Integer> list = new ArrayList<Integer>();
for(int x : nums){
list.add(x);
}
result.add(list);
// Add next permutation iterativel
for(int i = 1; i < factorial(nums.length); i++){
nextPermutation(nums);
}
return result;
}
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解