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
}
相关推荐
Kuo-Teng18 小时前
LeetCode 206: Reverse Linked List
java·算法·leetcode·职场和发展
做怪小疯子1 天前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法
Dream it possible!1 天前
LeetCode 面试经典 150_二叉树_二叉树展开为链表(74_114_C++_中等)
c++·leetcode·链表·面试·二叉树
做怪小疯子1 天前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展
sin_hielo1 天前
leetcode 2536
数据结构·算法·leetcode
flashlight_hi1 天前
LeetCode 分类刷题:203. 移除链表元素
算法·leetcode·链表
py有趣1 天前
LeetCode算法学习之数组中的第K个最大元素
学习·算法·leetcode
吗~喽1 天前
【LeetCode】将 x 减到 0 的最小操作数
算法·leetcode
stand_forever1 天前
PHP客户端调用由Go服务端GRPC接口
rpc·golang·php
flashlight_hi1 天前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表