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
}
相关推荐
YGGP4 小时前
【Golang】LeetCode 1. 两数之和
leetcode
唐梓航-求职中4 小时前
编程大师-技术-算法-leetcode-355. 设计推特
算法·leetcode·面试
唐梓航-求职中4 小时前
技术-算法-leetcode-1606. 找到处理最多请求的服务器(易懂版)
服务器·算法·leetcode
YGGP7 小时前
【Golang】LeetCode 128. 最长连续序列
leetcode
牛奔7 小时前
Go 如何避免频繁抢占?
开发语言·后端·golang
不老刘11 小时前
LiveKit 本地部署全流程指南(含 HTTPS/WSS)
golang·实时音视频·livekit
月挽清风15 小时前
代码随想录第十五天
数据结构·算法·leetcode
TracyCoder12317 小时前
LeetCode Hot100(34/100)——98. 验证二叉搜索树
算法·leetcode
Tony Bai17 小时前
再见,丑陋的 container/heap!Go 泛型堆 heap/v2 提案解析
开发语言·后端·golang
We་ct18 小时前
LeetCode 56. 合并区间:区间重叠问题的核心解法与代码解析
前端·算法·leetcode·typescript