random + swap
Key is changing the arrange of random number.
E.G. LC 384 Shuffle an array
1 2 3 4 5 6 7 8
|
vector<int> shuffle() { vector<int> shu(original); for(int i = 0; i < shu.size(); i++){ int ind = rand() % (shu.size() - i); swap(shu[i], shu[i+ind]); } return shu; }
|
近期评论