Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.
class Solution {
public:
Solution(vector<int>& nums) { v.assign(nums.begin(), nums.end()); }
/** Resets the array to its original configuration and return it. */
vector<int> reset() { return v; }
/** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> res{v};
for (int i = 0; i < res.size(); ++i) {
int t = i + rand() % (res.size() - i);
swap(res[i], res[t]);
}
return res;
}
private:
vector<int> v;
};