오늘 코드. 매우 간결하다!
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k = k % n
nums[:] = nums[n-k:] + nums[:n-k]
In-place로 작성해야하므로 nums에 넣어주었다.
in-place는 메모리를 더 사용하지 않고, 이미 주어진 메모리 안에서 문제를 해결해나가는 방식이라고 할 수 있다.
여기서 주의할 점은 두개이다.
1. k = k % n : 이 경우, 처음에는 Wrong answer이 나왔다. 알고보니 nums의 길이는 3인데 k는 5인 경우가 있더라... 그래서 k % n 을 해주어 k < n 이 되도록 해주었다./
2. nums[:] ! 처음에 nums = 로 해주어서 바뀌었는데 왜 안바뀌었다고 뜨지... 하고 생각했었다. nums[:] 주의할 것!
오늘 드디어 작심 삼일을 넘겼다. 예! 이제 작심 300일로 간드앗
'Leetcode' 카테고리의 다른 글
Leetcode 167. Two Sum II - Input array is sorted (0) | 2021.10.07 |
---|---|
Leetcode 283. Move Zeroes (0) | 2021.10.06 |
Leetcode 977. Squares of a Sorted Array - Two Pointers (0) | 2021.10.04 |
Leetcode 35. Search Insert Position - Binary Search (0) | 2021.10.03 |
Leetcode 278. First Bad Version - Binary Search Tree (0) | 2021.10.02 |