题意描述
- 给定一个数组,把这个数组当作参数传给solution的类,实现这个类的构造函数,reset()函数还原这个数组,shuffle函数是这数组随机洗牌
思路一
代码
1234567891011121314151617181920212223242526272829303132333435363738class Solution {public:Solution(vector<int> nums) {temp = nums;orig = nums;}/** Resets the array to its original configuration and return it. */vector<int> reset() {return orig;}/** Returns a random shuffling of the array. */vector<int> shuffle() {int size = temp.size();for(int i = 0; i < size; ++i) {int random = rand() % size;swap(i, random);}return temp;}void swap(int a, int b) {int tmp = temp[a];temp[a] = temp[b];temp[b] = tmp;}private:vector<int> temp;vector<int> orig;};/*** 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();*/
第384题 shuffle an array
坚持原创技术分享,您的支持将鼓励我继续创作!