Leetcode专题[数组]-46-全排列

盐柱语法
• 阅读 756

力扣链接:
https://leetcode-cn.com/probl...
解题思路:

  1. 回溯算法
func permute(nums []int) [][]int {
    res := [][]int{}
    backtrack(&res, nums, []int{})
    return res
}

func backtrack(res *[][]int, nums, path []int) {
    if len(path) == len(nums) {
        // 切片内存拷贝
        newPath := make([]int, len(path))
        copy(newPath, path)
        *res = append(*res, newPath)
        return
    }
    for i := 0; i < len(nums); i++ {
        if contains(path, nums[i]) {
            continue
        }
        path = append(path, nums[i])
        backtrack(res, nums, path)
        path = path[:len(path) - 1]
    }
}

func contains(nums []int, num int) bool {
    for _, v := range nums {
        if v == num {
            return true
        }
    }
    return false
}
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Stella981 Stella981
3年前
LeetCode 1009. 十进制整数的反码
原文链接: LeetCode1009.十进制整数的反码(https://my.oschina.net/ahaoboy/blog/3118044)https://leetcodecn.com/problems/complementofbase10integer/(https://www.oschina.net/action/GoToL
Stella981 Stella981
3年前
LeetCode 169. 求众数
原文链接: LeetCode169.求众数(https://my.oschina.net/ahaoboy/blog/3118038)https://leetcodecn.com/problems/majorityelement/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F
Stella981 Stella981
3年前
LeetCode 72. 编辑距离
原文链接: LeetCode72.编辑距离(https://my.oschina.net/ahaoboy/blog/3113850)https://leetcodecn.com/problems/editdistance/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2F
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
LeetCode 338. 比特位计数
原文链接: LeetCode338.比特位计数(https://my.oschina.net/ahaoboy/blog/3117631)https://leetcodecn.com/problems/countingbits/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%
Stella981 Stella981
3年前
LeetCode 227场周赛题解
【GiantPandaCV导语】这是LeetCode的第227场周赛的题解,本期考察的知识点有「暴力,字符串,二进制枚举」等等。比赛链接https://leetcodecn.com/contest/weeklycontest227/题目一:检查数组是否经排序和轮转得到
Easter79 Easter79
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Stella981 Stella981
3年前
LeetCode 生命游戏,不用新数组的方式
原文链接: LeetCode生命游戏,不用新数组的方式(https://my.oschina.net/ahaoboy/blog/4958278)https://leetcodecn.com/problems/gameoflife/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2
Stella981 Stella981
3年前
LeetCode 226场周赛题解
❝【GiantPandaCV导语】这是LeetCode第226场周赛题解,本周考察的知识点有枚举,贪心,前缀和,Manacher回文算法,动态规划,图论等。❞比赛链接https://leetcodecn.com/contest/weeklycontest226/最终Rank:23