Very simple C++ solution


  • 0
    Q
    class Solution {
        vector<int> _nums;
    public:
        Solution(vector<int> nums) : _nums(nums) {
            srand(time(0));
        }
        
        /** Resets the array to its original configuration and return it. */
        vector<int> reset() {
            return _nums;
        }
        
        /** Returns a random shuffling of the array. */
        vector<int> shuffle() {
            vector<int> nums(_nums);
            for (int i = 0; i < nums.size(); ++i) {
                swap(nums[i], nums[rand()%nums.size()]);
            }
            return nums;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.