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
}
相关推荐
wabs66621 分钟前
关于动态规划【力扣1143.最长公共子序列的思考】
算法·leetcode·动态规划
剑挑星河月42 分钟前
54.螺旋矩阵
java·算法·leetcode·矩阵
笨笨没好名字2 小时前
Leetcode刷题python3版第一周(下)
linux·算法·leetcode
想你依然心痛2 小时前
AtomCode在后端开发中的实战体验:Go微服务从零搭建
开发语言·微服务·golang
王老师青少年编程3 小时前
2026年6月GESP真题及题解(C++五级):排排坐
c++·题解·真题·gesp·五级·2026年6月·排排坐
开发小程序的之朴4 小时前
认识安企CMS - 系统概述
nginx·golang·系统架构
雨师@4 小时前
go语言项目--实例化(图书管理)--005
开发语言·后端·golang
Vect__4 小时前
Go 数据结构 slice 深度剖析
开发语言·数据结构·golang
geovindu4 小时前
go: Functional Options Pattern
开发语言·后端·设计模式·golang·函数式选项模式’·惯用法模式
青山木4 小时前
Hot 100 --- LRU 缓存
java·数据结构·算法·leetcode·链表·缓存·哈希