384. 打乱数组

 1 class Solution 
 2 {
 3     vector<int> vec;
 4 public:
 5     Solution(vector<int>& nums) 
 6     {
 7         vec = nums;
 8     }
 9     
10     /** Resets the array to its original configuration and return it. */
11     vector<int> reset() 
12     {
13         return vec;
14     }
15     
16     /** Returns a random shuffling of the array. */
17     vector<int> shuffle() 
18     {
19         vector<int> temp = vec;
20         for(int i = temp.size() - 1;i >= 0;i --)
21         {
22             swap(temp[rand() % (i + 1)],temp[i]);
23         }
24         return temp;
25     }
26 };

猜你喜欢

转载自www.cnblogs.com/yuhong1103/p/12787016.html
今日推荐