[shuffle] Clean C++ 8 lines (put srand() in constructor)


  • 0
    class Solution {
        vector<int>nums;
    public:
        Solution(vector<int> nums) {
            this->nums=nums;
            srand((unsigned)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>res(nums.begin(),nums.end());
            for(int i=0;i<res.size();i++)
                swap(res[i],res[rand()%res.size()]);
            return res;
        }
    };
    

Log in to reply
 

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