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
}
相关推荐
鹏北海-RemHusband8 小时前
Go 语言进阶笔记 — 面向 JS/TS 前端开发者
笔记·golang
罗超驿13 小时前
9.LeetCode 209. 长度最小的子数组 | 滑动窗口专题详解
java·算法·leetcode·面试
水蓝烟雨13 小时前
0135. 分发糖果
算法·leetcode
如竟没有火炬14 小时前
乘法表中第K小的数——二分
开发语言·数据结构·python·算法·leetcode·职场和发展·动态规划
诚威_lol_中大努力中16 小时前
Hot-146 LRU(最近最少使用Least Recent Use)缓存
leetcode
x_xbx17 小时前
LeetCode:739. 每日温度
算法·leetcode·职场和发展
Starry-sky(jing)17 小时前
Hermes Agent 接入 Qwen3.7-Max 报 401?OpenCode Go 模型路由源码级排查与修复
开发语言·人工智能·chrome·golang
鹏北海-RemHusband17 小时前
Go 语言基础笔记 — 面向 JS/TS 前端开发者
笔记·golang
圣保罗的大教堂18 小时前
leetcode 3121. 统计特殊字母的数量 II 中等
leetcode
圣保罗的大教堂18 小时前
leetcode 3120. 统计特殊字母的数量 I 简单
leetcode