Golang | Leetcode Golang题解之第384题打乱数组

题目:

Golang | Leetcode Golang题解之第384题打乱数组_第1张图片

题解:

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
}

你可能感兴趣的:(经验分享,Golang,Leetcode,题解)