https://leetcode.com/explore/interview/card/top-interview-questions-easy/98/design/670/
/**
* @param {number[]} nums
*/
var Solution = function(nums) {
this.original = [...nums];
this.nums = nums;
};
/**
* @return {number[]}
*/
Solution.prototype.reset = function() {
this.nums = [...this.original];
return this.nums;
};
/**
* @return {number[]}
*/
Solution.prototype.shuffle = function() {
for(let i = this.nums.length - 1; i > 0; i--) {
const random = Math.floor(Math.random() * (i + 1));
[this.nums[i], this.nums[random]] = [this.nums[random], this.nums[i]];
}
return this.nums;
};
/**
* Your Solution object will be instantiated and called as such:
* var obj = new Solution(nums)
* var param_1 = obj.reset()
* var param_2 = obj.shuffle()
*/
GitHub : https://github.com/developeSHG/Algorithm-LeetCode/tree/main/shuffle-an-array
'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] (Array) Lv Easy. Contains Duplicate (0) | 2023.02.06 |
---|---|
[LeetCode] (Array) Lv Easy. Rotate Array (0) | 2023.01.31 |
[LeetCode] (Math) Lv Easy. Fizz Buzz (0) | 2023.01.22 |
[LeetCode] (Dynamic Programming) Lv Easy. Climbing Stairs (0) | 2023.01.22 |
[LeetCode] (Sorting and Searching) Lv Easy. First Bad Version (0) | 2023.01.22 |
댓글