Leetcode 384 Solution

This article provides solution to leetcode question 384 (shuffle-an-array)

https://leetcode.com/problems/shuffle-an-array

Solution

class Solution {
    vector<int> m_nums;

public:
    Solution(vector<int> nums) {
        m_nums = nums;
    }

    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
        return m_nums;
    }

    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
        vector<int> res = m_nums;

        for (int i = 0; i < res.size(); i++)
        {
            int k = rand() % (res.size() - i);
            swap(res[i], res[i + k]);
        }

        return res;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * vector<int> param_1 = obj.reset();
 * vector<int> param_2 = obj.shuffle();
 */