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
}
相关推荐
Scabbards_1 小时前
面试Leetcode - Heap 堆
java·leetcode·面试
ValhallaCoder4 小时前
Leetcode-hot100(2026.08.17)
python·算法·leetcode
xcLeigh4 小时前
Go入门:无类型常量与类型常量的区别
服务器·开发语言·golang
泡沫冰@4 小时前
GO 语言基础
开发语言·算法·golang
青 春 记 忆8 小时前
LeetCode 104. 二叉树的最大深度|Python 解法详解
python·算法·leetcode
骇客野人9 小时前
电商商城微服务架构设计方案(SpringCloud/Go+Vue3+MySQL+ES+RocketMQ)
spring cloud·微服务·golang
码农大叔的博客10 小时前
golang示例:for九九乘法表
开发语言·算法·golang
圣殿骑士-Khtangc10 小时前
Go-sync-Pool对象池最佳实践与源码分析
golang
互联网中的一颗神经元11 小时前
01. Go 内存管理全景架构
java·jvm·golang
有脚就行11 小时前
第24篇-Go-gRPC推理服务-高性能跨语言通信
开发语言·人工智能·后端·golang