일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 109. Convert Sorted List to Binary Search Tree
- 715. Range Module
- 30. Substring with Concatenation of All Words
- DWG
- concurrency
- 프로그래머스
- iterator
- Convert Sorted List to Binary Search Tree
- t1
- Decorator
- 운영체제
- kaggle
- Generator
- LeetCode
- 315. Count of Smaller Numbers After Self
- Class
- 컴퓨터의 구조
- data science
- 43. Multiply Strings
- Python Code
- 밴픽
- Regular Expression
- 시바견
- Substring with Concatenation of All Words
- Python Implementation
- Python
- shiba
- attribute
- Protocol
- 파이썬
Archives
- Today
- Total
Scribbling
LeetCode: 384. Shuffle an Array 본문
Refer to the below post for Knuth Shuffle.
https://focalpoint.tistory.com/253
Shuffle Algorithm: Knuth Shuffle
This post is about Knuth Shuffle, a shuffle algorithm. - This algorithm allows random shuffle, giving all permuations of array equal likelihood. - No need for additional arrays, so memory complexity..
focalpoint.tistory.com
class Solution:
def __init__(self, nums: List[int]):
self.nums = nums
def reset(self) -> List[int]:
return self.nums
def shuffle(self) -> List[int]:
import copy
import random
self.temp = copy.deepcopy(self.nums)
for i in range(len(self.temp)-1):
j = random.randint(i, len(self.temp)-1)
self.temp[i], self.temp[j] = self.temp[j], self.temp[i]
return self.temp
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 454. 4Sum II (0) | 2022.01.16 |
---|---|
LeetCode: 395. Longest Substring with At Least K Repeating Characters (0) | 2022.01.16 |
LeetCode: 378. Kth Smallest Element in a Sorted Matrix (0) | 2022.01.14 |
LeetCode: 341. Flatten Nested List Iterator (0) | 2022.01.13 |
LeetCode: 334. Increasing Triplet Subsequence (0) | 2022.01.11 |