Problem:
Shuffle a set of numbers without duplicates.
Example:
// Init an array with set 1, 2, and 3.
int[] nums = {1,2,3};
Solution solution = new Solution(nums);
// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned. solution.shuffle();
// Resets the array back to its original configuration [1,2,3].
solution.reset();
// Returns the random shuffling of array [1,2,3].
solution.shuffle();
-Summary-
class Solution:
def __init__(self, nums: List[int]):
self.origin = nums
def reset(self) -> List[int]:
"""
Resets the array to its original configuration and return it.
"""
return self.origin
def shuffle(self) -> List[int]:
"""
Returns a random shuffling of the array.
"""
return random.sample(self.origin, len(self.origin))
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
Another solution from the leetcode discussion.
leetcode.com/problems/shuffle-an-array/discuss/380991/Simply-Simple-Python-Solution-with-proof-O(n)
Simply Simple Python Solution with proof - O(n) - LeetCode Discuss
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
모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]
'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글
LeetCode. Min Stack (0) | 2020.06.26 |
---|---|
LeetCode. Reverse Bits (0) | 2020.06.22 |
LeetCode. Pascal's Triangle (0) | 2020.06.21 |
LeetCode. Missing Number [Bit] (0) | 2020.06.19 |
LeetCode.Hamming Distance [Bit] (0) | 2020.06.18 |
댓글