LeetCode 384. Shuffle an Array
2022.10.10
問題
https://leetcode.com/problems/shuffle-an-array/
typescript
class Solution {
private nums: number[]
private origin: number[]
constructor(nums: number[]) {
this.nums = nums
this.origin = [...nums]
}
reset(): number[] {
return this.origin
}
shuffle(): number[] {
for (let i = this.origin.length - 1; i >= 0; i--) {
const random = Math.floor(Math.random() * (i+1))
const tmp = this.nums[i]
this.nums[i] = this.nums[random]
this.nums[random] = tmp
}
return this.nums
}
}
- クラスとメソッドの実装
- shuffleは適当に入れ替えるのみ