forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
384.cpp
63 lines (53 loc) · 1.44 KB
/
384.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
public:
Solution(vector<int> nums) : _nums(nums) {
srand(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;
for(int i = res.size() - 1;i >= 1;i--){
int random = rand() % (i + 1);
int tp = res[i];
res[i] = res[random];
res[random] = tp;
}
return res;
}
private:
vector<int> _nums;
};
/**
* 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();
*/
class Solution {
public:
Solution(vector<int> nums) : _nums(nums) , reserve(nums) {
srand(time(0));
}
/** Resets the array to its original configuration and return it. */
vector<int> reset() {
_nums = reserve;
return _nums;
}
/** Returns a random shuffling of the array. */
vector<int> shuffle() {
for(int i = _nums.size() - 1;i >= 1;i--){
int random = rand() % (i + 1);
int tp = _nums[i];
_nums[i] = _nums[random];
_nums[random] = tp;
}
return _nums;
}
private:
vector<int> _nums;
vector<int> reserve;
};