Golang | Leetcode Golang题解之第384题打乱数组

题目:

题解:

Go 复制代码
type Solution struct {
    nums, original []int
}

func Constructor(nums []int) Solution {
    return Solution{nums, append([]int(nil), nums...)}
}

func (s *Solution) Reset() []int {
    copy(s.nums, s.original)
    return s.nums
}

func (s *Solution) Shuffle() []int {
    n := len(s.nums)
    for i := range s.nums {
        j := i + rand.Intn(n-i)
        s.nums[i], s.nums[j] = s.nums[j], s.nums[i]
    }
    return s.nums
}
相关推荐
longlongzihan11 小时前
链表找环:Floyd 判圈算法(LeetCode 141 & 142 )
c++·算法·leetcode·链表
yi01112 小时前
LeetCode 643. 子数组最大平均数 I|从暴力枚举到滑动窗口
人工智能·笔记·python·算法·leetcode·滑动窗口
FfHUCisI13 小时前
Go 性能分析工具 pprof:CPU、Heap 与 Goroutine 实战
数据库·golang
念何架构之路13 小时前
go-grpc核心抽象接口
开发语言·后端·golang
java_nnnn14 小时前
LeetCode刷题双指针(有效三角形的个数+三数之和)Java
算法·leetcode·职场和发展
小小龙学IT14 小时前
etcd 深度解析:Go 分布式一致性 KV 存储与 Raft 共识实战
分布式·golang·etcd
谢亮_vipxieliang14 小时前
Go Gin 中间件与参数验证:从入门到实战
中间件·golang·gin
All for pursuit.15 小时前
【动态规划-6】96.不同的二叉搜索树
数据结构·c++·算法·leetcode·动态规划
All for pursuit.17 小时前
【并查集-2】399.除法求值
数据结构·c++·算法·leetcode
Nil20818 小时前
leetcode 416分割等和子集
算法·leetcode·职场和发展