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
}
相关推荐
幼稚园的山代王2 小时前
go语言了解
开发语言·后端·golang
zhuyasen2 小时前
踩坑实录:Go 1.25.x 编译的 exe 在 Windows 提示“此应用无法运行”
windows·golang
Lris-KK3 小时前
力扣Hot100--94.二叉树的中序遍历、144.二叉树的前序遍历、145.二叉树的后序遍历
python·算法·leetcode
坚持编程的菜鸟4 小时前
LeetCode每日一题——螺旋矩阵
c语言·算法·leetcode·矩阵
(●—●)橘子……4 小时前
记力扣2009:使数组连续的最少操作数 练习理解
数据结构·python·算法·leetcode
GalaxyPokemon4 小时前
LeetCode - 1171.
算法·leetcode·链表
iナナ4 小时前
Java优选算法——位运算
java·数据结构·算法·leetcode
Han.miracle5 小时前
数据结构二叉树——层序遍历&& 扩展二叉树的左视图
java·数据结构·算法·leetcode
L_09078 小时前
【Algorithm】Day-4
c++·算法·leetcode
代码充电宝8 小时前
LeetCode 算法题【简单】20. 有效的括号
java·算法·leetcode·面试·职场和发展