Easy C++ solution


  • 0
    M
    class Solution {
    public:
    vector<int> original;
    Solution(vector<int> nums) {
        original = nums;
    }
    
    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
        return original;
    }
    
    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
        vector<int> origin = original;
        int n = original.size();
        for(int i=0; i<n; i++){
            int ran = rand() % (n-i);
            int temp = origin[n-i-1];
            origin[n-i-1] = origin[ran];
            origin[ran] = temp;
        }
        return origin;
    }
    

    };

    /**
     * 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();
     */

Log in to reply
 

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