C++ easy solution


  • 0
    A
    class Solution {
    private: 
    	vector<int> mynums;
    	
    public:
        Solution(vector<int> nums) {
    		srand(time(NULL));
    		mynums = nums;
        }
        
        /** Resets the array to its original configuration and return it. */
        vector<int> reset() {
            return mynums;
        }
        
        /** Returns a random shuffling of the array. */
        vector<int> shuffle() {
            vector<int> permut(mynums);
    		int n = permut.size();
    		for (int i = 0; i < n; ++i){
    			int rand_num = rand() % (n - i) + i;
    			if (rand_num != i) swap(permut[i], permut[rand_num]);
    		}
    		return permut;
        }
    	
    	/* fast swap function */
    	void swap(int& a, int& b){
    		a ^= b ^= a ^= b;
    	}
    };
    

Log in to reply
 

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