shuffle_array

  • 2022-12-14
  • 浏览 (544)

shuffle_array.go 源码

package main

import (
	"math/rand"
)

//打乱数组

type Solution struct {
	nums []int
}

func Constructor(nums []int) Solution {
	return Solution{nums}
}

func (this *Solution) Reset() []int {
	return this.nums
}

func (this *Solution) Shuffle() []int {
	data := make([]int, len(this.nums))
	copy(data, this.nums)
	for i := 0; i < len(data); i++ {
		j := rand.Intn(len(data))
		data[i], data[j] = data[j], data[i]
	}
	return data
}

你可能感兴趣的文章

best_time_buy_sell_stock

best_time_buy_sell_stock_ii

climb_stairs

0  赞