bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 384. Shuffle an Array

384. Shuffle an Array

Leetcode Solutions

Fisher-Yates Shuffle Algorithm

  1. Initialize the original array nums and store it.
  2. To reset the array, simply return the stored original array.
  3. To shuffle the array, iterate over the array from the first index to the second to last index.
  4. For each index i, generate a random index j such that i <= j < n where n is the length of the array.
  5. Swap the elements at indices i and j.
  6. Return the shuffled array.
UML Thumbnail

Brute Force Shuffle Algorithm

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...