본문 바로가기
Coding Test/LeetCode

[LeetCode] (Design) Lv Easy. Shuffle an Array

by song.ift 2023. 1. 25.

https://leetcode.com/explore/interview/card/top-interview-questions-easy/98/design/670/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

 

/**
 * @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

 

GitHub - developeSHG/Algorithm-LeetCode: 릿코드 소스코드

릿코드 소스코드. Contribute to developeSHG/Algorithm-LeetCode development by creating an account on GitHub.

github.com

 

댓글