[leetcode]47. Permutations II@Java解题报告
2017-08-19 11:10
519 查看
https://leetcode.com/problems/permutations-ii/description/
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
package go.jacob.day819;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 47. Permutations II
*
* @author Jacob
* 思路:与46. Permutations解法类似。区别是,该题需要单独建立一个boolean数组
* 用来记录nums中的元素是否被使用(是否已经加入list集合)
*/
public class Demo2 {
List<List<Integer>> res;
public List<List<Integer>> permuteUnique(int[] nums) {
res = new ArrayList<List<Integer>>();
if (nums == null || nums.length < 1)
return res;
Arrays.sort(nums);
ArrayList<Integer> list = new ArrayList<Integer>();
boolean[] used = new boolean[nums.length];
solve(list, nums, used);
return res;
}
private void solve(ArrayList<Integer> list, int[] nums, boolean[] used) {
if (list.size() == nums.length) {
res.add(new ArrayList<Integer>(list));
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i])
continue;
/*
* 只需要判断i和i-1(而不需要判断i与i-2...) 相同的元素一定是相邻的。
* 如果i-2,i-1,i相同,那么在上一轮循环就已经判断了i-1,i,本轮循环不需要重复判断
*/
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])
continue;
list.add(nums[i]);
used[i]=true;
solve(list, nums, used);
used[i]=false;
list.remove(list.size()-1);
}
}
}
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] ]
package go.jacob.day819;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 47. Permutations II
*
* @author Jacob
* 思路:与46. Permutations解法类似。区别是,该题需要单独建立一个boolean数组
* 用来记录nums中的元素是否被使用(是否已经加入list集合)
*/
public class Demo2 {
List<List<Integer>> res;
public List<List<Integer>> permuteUnique(int[] nums) {
res = new ArrayList<List<Integer>>();
if (nums == null || nums.length < 1)
return res;
Arrays.sort(nums);
ArrayList<Integer> list = new ArrayList<Integer>();
boolean[] used = new boolean[nums.length];
solve(list, nums, used);
return res;
}
private void solve(ArrayList<Integer> list, int[] nums, boolean[] used) {
if (list.size() == nums.length) {
res.add(new ArrayList<Integer>(list));
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i])
continue;
/*
* 只需要判断i和i-1(而不需要判断i与i-2...) 相同的元素一定是相邻的。
* 如果i-2,i-1,i相同,那么在上一轮循环就已经判断了i-1,i,本轮循环不需要重复判断
*/
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])
continue;
list.add(nums[i]);
used[i]=true;
solve(list, nums, used);
used[i]=false;
list.remove(list.size()-1);
}
}
}
相关文章推荐
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
- ?47. Permutations II leetcode java
- [leetcode]126. Word LadderII@Java解题报告
- JAVA学习44_leetCode解题报告之Copy List with Random Pointer
- [Leetcode] 229. Majority Element II 解题报告
- [LeetCode]Permutations,解题报告
- [leetcode] 364. Nested List Weight Sum II 解题报告
- [leetcode]50. Pow(x, n)@Java解题报告
- 【LeetCode】Palindrome Partitioning II 解题报告
- [LeetCode] Pascal's Triangle II 解题报告
- [Leetcode] 768. Max Chunks To Make Sorted II 解题报告
- 【LeetCode】275.H-Index II(Medium)解题报告
- LeetCode-Basic Calculator II-解题报告
- [LeetCode]47. Permutations II && DFS及剪枝
- [LeetCode 46 & 47] Permutations I & II
- 【LeetCode】Hamming Distance 解题报告(java & python)
- [Leetcode] 119. Pascal's Triangle II 解题报告
- LeetCode 46/47. Permutations i, ii
- [leetcode]46. Permutations@Java解题报告
- LeetCode 227. Basic Calculator II 解题报告【python】