如何求解LeetCode 47题:全排列II的重复排列问题?

2026-04-28 14:402阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计571个文字,预计阅读时间需要3分钟。

如何求解LeetCode 47题:全排列II的重复排列问题?

解决方法,先构造一个hashmap,key是元素,value是元素的出现次数,然后使用回溯法来解决问题。

一、题目大意标签:搜索链接:https://leetcode.cn/problems/permutations-ii给定一个可包含重复数字的序列nums,返回所有不重复的全排列。

二、给定条件nums:一个包含可重复数字的序列

三、示例输入:nums=[1,1,2]输出:[[1,1,2],[1,2,1],[2,1,1]]

解决方法,先构造一个hashmap,key是元素,value是元素的个数,然后再用回溯法来解决 一、题目大意

标签: 搜索

leetcode.cn/problems/permutations-ii

给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
示例 1:

输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]

示例 2:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

提示:

如何求解LeetCode 47题:全排列II的重复排列问题?

  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10
二、解题思路

用回溯法解决全排列问题,给定的数组中元素有重复,因此用回溯法执行后的全排列结果中会有重复的,如下图所示。

解决方法,先构造一个hashmap,key是元素,value是元素的个数,然后再用回溯法来解决

三、解题方法 3.1 Java实现

public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); // 构造一个hashmap Map<Integer, Integer> countMap = new HashMap<>(); for (int n : nums) { int count = countMap.getOrDefault(n, 0); countMap.put(n, count + 1); } dfs(countMap, nums.length, new LinkedList<>(), ans); return ans; } void dfs(Map<Integer, Integer> countMap, int total, Deque<Integer> perm, List<List<Integer>> ans) { // 使用双端队列 if (perm.size() == total) { ans.add(new ArrayList<>(perm)); } for (Map.Entry<Integer, Integer> tmp : countMap.entrySet()) { if (tmp.getValue() > 0) { int oldValue = tmp.getValue(); perm.offerFirst(tmp.getKey()); tmp.setValue(tmp.getValue() - 1); dfs(countMap, total, perm, ans); tmp.setValue(oldValue); perm.pollFirst(); } } } } 四、总结小记

  • 2022/6/12 来记录结果的类型要用双端队列

本文共计571个文字,预计阅读时间需要3分钟。

如何求解LeetCode 47题:全排列II的重复排列问题?

解决方法,先构造一个hashmap,key是元素,value是元素的出现次数,然后使用回溯法来解决问题。

一、题目大意标签:搜索链接:https://leetcode.cn/problems/permutations-ii给定一个可包含重复数字的序列nums,返回所有不重复的全排列。

二、给定条件nums:一个包含可重复数字的序列

三、示例输入:nums=[1,1,2]输出:[[1,1,2],[1,2,1],[2,1,1]]

解决方法,先构造一个hashmap,key是元素,value是元素的个数,然后再用回溯法来解决 一、题目大意

标签: 搜索

leetcode.cn/problems/permutations-ii

给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
示例 1:

输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]

示例 2:

输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

提示:

如何求解LeetCode 47题:全排列II的重复排列问题?

  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10
二、解题思路

用回溯法解决全排列问题,给定的数组中元素有重复,因此用回溯法执行后的全排列结果中会有重复的,如下图所示。

解决方法,先构造一个hashmap,key是元素,value是元素的个数,然后再用回溯法来解决

三、解题方法 3.1 Java实现

public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); // 构造一个hashmap Map<Integer, Integer> countMap = new HashMap<>(); for (int n : nums) { int count = countMap.getOrDefault(n, 0); countMap.put(n, count + 1); } dfs(countMap, nums.length, new LinkedList<>(), ans); return ans; } void dfs(Map<Integer, Integer> countMap, int total, Deque<Integer> perm, List<List<Integer>> ans) { // 使用双端队列 if (perm.size() == total) { ans.add(new ArrayList<>(perm)); } for (Map.Entry<Integer, Integer> tmp : countMap.entrySet()) { if (tmp.getValue() > 0) { int oldValue = tmp.getValue(); perm.offerFirst(tmp.getKey()); tmp.setValue(tmp.getValue() - 1); dfs(countMap, total, perm, ans); tmp.setValue(oldValue); perm.pollFirst(); } } } } 四、总结小记

  • 2022/6/12 来记录结果的类型要用双端队列