LeetCode 打卡 Day 26 — 全排列

1、题目

2、题解

看完题目后感觉跟之前的好多道题类似又不那么类似,第一瞬间想到了回溯的方法,感觉回溯有时候就像是个高级递归,代码如下:

var res [][]int

func permute(nums []int) [][]int {
    res = [][]int{}

    if len(nums)==1{
        return append(res, nums)
    }
    wholeArrangement([]int{}, nums)
    return res
}

func wholeArrangement(now, other []int) {
    n := len(other)

    if n==1 {
        now_ := append([]int{}, now...)
        other_ := append([]int{}, other...)
        res = append(res, append(now_, other_...))
    }
    
    for i:=0; i<len(other); i++{
        now_ := append([]int{}, now...)
        other_ := append([]int{}, other...)
        wholeArrangement(append(now_, other_[i]), append(other_[:i], other_[i+1:]...))
    }
}

提交结果如下

 

时间确实很省啊,但是为了防止数组被覆盖每次都要新建数组,这个内存消耗是真的大 😹

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值