class Solution { private: vector<int> arr, idx; public: Solution(vector<int> nums) { srand(time(NULL)); idx.resize(nums.size()); arr.resize(nums.size()); for(int i = 0; i < nums.size(); ++i) { arr[i] = nums[i]; idx[i] = nums[i]; } } /** Resets the array to its original configuration and return it. */ vector<int> reset() { for(int i = 0; i < arr.size(); ++i) { arr[i] = idx[i]; } return arr; } /** Returns a random shuffling of the array. */ vector<int> shuffle() { for(int i = arr.size() - 1; i >= 0; --i) { int j = rand() % (i + 1); swap(arr[i], arr[j]); } return arr; } };
初始化种子一定要在类的构造函数里
原文:http://www.cnblogs.com/ddddddwwwxx/p/5878847.html