일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- Protocol
- 파이썬
- iterator
- 컴퓨터의 구조
- shiba
- 30. Substring with Concatenation of All Words
- Python Implementation
- 715. Range Module
- concurrency
- 315. Count of Smaller Numbers After Self
- Convert Sorted List to Binary Search Tree
- Regular Expression
- kaggle
- Decorator
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- 시바견
- t1
- 43. Multiply Strings
- Class
- Substring with Concatenation of All Words
- DWG
- data science
- 밴픽
- Python Code
- Generator
- 프로그래머스
- 운영체제
- Python
- attribute
Archives
- Today
- Total
Scribbling
LeetCode: 88. Merge Sorted Array 본문
The key here is that we need to push elements from nums2 to nums1 while not disturbing the existing elements in nums1.
So, put the elems from nums2 to nums1 from the end.
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
p1, p2 = m - 1, n - 1
for i in range(m+n-1, -1, -1):
p1_val = nums1[p1] if p1 >= 0 else -float('inf')
p2_val = nums2[p2] if p2 >= 0 else -float('inf')
if p1_val >= p2_val:
nums1[i] = p1_val
p1 -= 1
else:
nums1[i] = p2_val
p2 -= 1
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 100. Same Tree (0) | 2021.10.11 |
---|---|
LeetCode: 90. Subsets II (0) | 2021.10.10 |
LeetCode: 98. Validate Binary Search Tree (0) | 2021.10.09 |
LeetCode: 99. Recover Binary Search Tree (0) | 2021.10.08 |
LeetCode: 89. Gray Code (0) | 2021.10.08 |