要求:洗牌随机
思路:在[i,n-1]的随机选一个与i交换
class Solution {
public:
vector<int> origin;
vector<int> current;
int n;
Solution(vector<int>& nums) {
origin = nums;
current = nums;
n = nums.size();
}
vector<int> reset() {
current = origin;
return current;
}
vector<int> shuffle() {
for (int i = 0; i < n; i++) {
int tmp;
int target = i + rand() % (n - i);
tmp = current[i];
current[i] = current[target];
current[target] = tmp;
}
return current;
}
};
/**
* 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();
*/