您的位置:首页 > 其它

Leetcode 47. Permutations II

2017-01-05 11:44 393 查看
public class Solution {
public static void backTrack(int[] nums, boolean[] used, List<Integer> tmp, List<List<Integer>> res) {
if (tmp.size() == nums.length) {
res.add(new ArrayList<>(tmp));
return;
}
else {
for (int i=0; i<nums.length; i++) {
if (used[i] || i>0 && nums[i-1]==nums[i] && !used[i-1]) continue;
used[i] = true;
tmp.add(nums[i]);
backTrack(nums, used, tmp, res);
used[i] = false;
tmp.remove(tmp.size()-1);
}
}
}

public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums); // always sort when there are duplicates in the array
List<List<Integer>> res = new ArrayList<>();
backTrack(nums, new boolean[nums.length], new ArrayList<>(), res);
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: