美文网首页
LeetCode - 47. 全排列 II

LeetCode - 47. 全排列 II

作者: huxq_coder | 来源:发表于2020-09-25 09:50 被阅读0次

给定一个可包含重复数字的序列,返回所有不重复的全排列。

示例:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/permutations-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

算法

思考:返回所有不重复的全排列
结果集不重复:
1、根据 47.全排列,得到所有结果后去重
2、过程中去重(推荐)

swift
  • 结果去重
// 结果去重,利用set元素不重复特性保存结果集
 
 var result = Set<[Int]>()
 func permuteUnique(_ nums: [Int]) -> [[Int]] {
     if nums.count == 0 {
         return [[Int]]()
     }
     let used = [Bool](repeating: false, count: nums.count)
     dfs(nums, nums.count, 0, [Int](), used)
     return [[Int]](result)
 }

 func dfs(_ nums: [Int],_ length: Int, _ depth: Int, _ path: [Int], _ used: [Bool]) {
     if length == depth {
         result.insert(path)
         return
     }
     for i in 0..<length {
         if !used[i] {
             var newPath = path
             newPath.append(nums[i])
             
             var newUsed = used
             newUsed[i] = true
             dfs(nums, length, depth+1, newPath, newUsed)
         }
     }
 }
  • 过程去重
/**
 过程去重
 参考题解: https://leetcode-cn.com/problems/permutations-ii/solution/hui-su-suan-fa-python-dai-ma-java-dai-ma-by-liwe-2/
 */

var result = [[Int]]()
func permuteUnique(_ nums: [Int]) -> [[Int]] {
    // 过滤边界
    if nums.count == 0 {
        return result
    }
    // 排序
    let nums = nums.sorted()
    // 访问标记位
    var used = [Bool](repeating: false, count: nums.count)
    // 访问路径
    var path = [Int]()
    dfs(nums, nums.count, 0, &path, &used)
    return result
}

func dfs(_ nums: [Int],_ length: Int, _ depth: Int, _ path: inout [Int], _ used: inout [Bool]) {
    // 递归终结条件
    if length == depth {
        result.append([Int](path))
        return
    }
    for i in 0..<length {
        // 未被访问
        if !used[i] {
            // 剪枝条件
            // i > 0 : 0是第一个,没有之前的元素可以比较是否重复,保证后面的nums[i-1]有效
            // used[i-1] 或者 !used[i-1] 的选择可以参考题解的最后解释
            if i > 0 && nums[i] == nums[i-1] && !used[i-1] {
                continue
            }
            // 添加路径
            path.append(nums[i])
            // 添加标记位 已访问
            used[i] = true
            dfs(nums, length, depth+1, &path, &used)
            // 回溯
            used[i] = false
            path.removeLast()
        }
    }
}

GitHub:https://github.com/huxq-coder/LeetCode
欢迎star

相关文章

  • 47. 全排列 II

    47. 全排列 II[https://leetcode.cn/problems/permutations-ii/]...

  • 47. 全排列 II、39. 组合总和、40. 组合总和 II

    回溯的题 47. 全排列 II[https://leetcode-cn.com/problems/permutat...

  • LeetCode - 47. 全排列 II

    给定一个可包含重复数字的序列,返回所有不重复的全排列。 示例: 输入: [1,1,2]输出:[[1,1,2],[1...

  • [LeetCode][M] 47. 全排列 II

    给定一个可包含重复数字的序列,返回所有不重复的全排列。 示例: 输入: [1,1,2]输出:[[1,1,2],[1...

  • YC-常考的题目

    46. 全排列 47. 全排列 II 有条件的全排列,打印出[1,2,2,3,4,5]的所有4不在头并且3和5不挨...

  • 搜索(二)回溯

    一、题目总结 基础问题 46.全排列 77.组合 78.子集 39.组合求和 47.全排列 II(重复元素) 90...

  • leetcode题目47. 全排列 II(java)

    题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列。 示例 代码

  • LeetCode 力扣 47. 全排列 II

    题目描述(中等难度) 和上一道题类似,不同之处就是给定的数字中会有重复的,这样的话用之前的算法会产出重复的序列。例...

  • 47.全排列II

    题目给定一个可包含重复数字的序列,返回所有不重复的全排列。 示例:****输入: [1,1,2]输出:[[1,1,...

  • 47. 全排列 II

    给定一个可包含重复数字的序列,返回所有不重复的全排列。 示例: 思路 python3解法 来源:力扣(LeetCo...

网友评论

      本文标题:LeetCode - 47. 全排列 II

      本文链接:https://www.haomeiwen.com/subject/yawxuktx.html