-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshuffleanArray.cpp
More file actions
36 lines (31 loc) · 866 Bytes
/
shuffleanArray.cpp
File metadata and controls
36 lines (31 loc) · 866 Bytes
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
// Source: https://leetcode.com/problems/shuffle-an-array/
// Author: Miao Zhang
// Date: 2021-02-06
class Solution {
public:
Solution(vector<int>& nums) {
nums_ = std::move(nums);
}
/** 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> nums(nums_);
int n = nums_.size();
for (int i = 0; i < n; i++) {
int j = rand() % ( n - i) + i;
swap(nums[i], nums[j]);
}
return nums;
}
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();
*/