-
[LeetCode] 384. Shuffle an ArrayLeetCode 2021. 7. 20. 21:20728x90
https://leetcode.com/problems/shuffle-an-array/
Shuffle an Array - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Shuffle an Array.

각기 다른 수가 들어있는 vector 주어졌을때,
"shuffle" 명령이 들어오면 이를 섞고, "reset" 명령이 들어오면 본래의 vector를 return 하는 문제다.
매우매우매우매우 간단하다.
algorithm 헤더에 random_shuffle 이 구현되어 있는데 갖다 쓰자.
class Solution { vector<int> org; public: Solution(vector<int>& nums) {this->org = nums;} vector<int> reset() {return this->org;} vector<int> shuffle() { vector<int> ret = this->org; random_shuffle(ret.begin(),ret.end()); return ret; } };+ 사실 random_shuffle을 갖다 쓰기 전에 직접 구현을 해보았었다. (모두는 아니지만.)
shuffle을 제외한 부분은 모두 같고, shuffle 부분은 랜덤한 인덱스를 뽑아 swap을 하는 식으로 섞었는데..
무슨 기준인지 모르겠는데 이렇게 하면 fail이 뜬다. 랜덤이라 정답체크를 어떻게 하는지 모르겠다.
내가 알기로는 random_shuffle도 이런식으로 random index를 shuffle하는것으로 알고있는데 말이다.
'LeetCode' 카테고리의 다른 글
[LeetCode] 915. Partition Array into Disjoint Intervals (0) 2021.07.22 [LeetCode] 838. Push Dominoes (0) 2021.07.21 [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree (0) 2021.07.19 [LeetCode] 611. Valid Triangle Number (0) 2021.07.15 [LeetCode] 791. Custom Sort String (0) 2021.07.14