Golang | Leetcode Golang题解之第47题全排列II

题目:

题解:

Go 复制代码
func permuteUnique(nums []int) (ans [][]int) {
    sort.Ints(nums)
    n := len(nums)
    perm := []int{}
    vis := make([]bool, n)
    var backtrack func(int)
    backtrack = func(idx int) {
        if idx == n {
            ans = append(ans, append([]int(nil), perm...))
            return
        }
        for i, v := range nums {
            if vis[i] || i > 0 && !vis[i-1] && v == nums[i-1] {
                continue
            }
            perm = append(perm, v)
            vis[i] = true
            backtrack(idx + 1)
            vis[i] = false
            perm = perm[:len(perm)-1]
        }
    }
    backtrack(0)
    return
}
相关推荐
圣殿骑士-Khtangc17 分钟前
Go泛型编程实战从类型约束到泛型数据结构
golang
evans在进步2 小时前
LeetCode 394:字符串解码——Java 单栈模拟与嵌套解析详解
java·python·leetcode
Nil2082 小时前
leetcode 189轮转数组
数据结构·算法·leetcode
FfHUCisI2 小时前
Golang - 信号量模式(Semaphore Pattern)
开发语言·后端·golang
吃着火锅x唱着歌3 小时前
LeetCode 3885.设计事件管理器
算法·leetcode·职场和发展
土司大王3 小时前
LeetCode hto100——字母异位词分组
java·算法·leetcode
土司大王4 小时前
LeetCode hot100——两数之和
数据结构·算法·leetcode
Navigator_Z5 小时前
LeetCode //C - 1200. Minimum Absolute Difference
c语言·算法·leetcode
wabs6665 小时前
关于字符串【力扣344.反转字符串的思考】
数据结构·算法·leetcode
Nil2086 小时前
leetcode 73矩阵置0
算法·leetcode·矩阵