//GitHub https://github.com/vellimole0621
vellimole0621 - Overview
キム・ヒョンギュ . vellimole0621 has 6 repositories available. Follow their code on GitHub.
github.com
Explore - LeetCode
LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.
leetcode.com
:
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
=> 수로 이루어진 두 배열이 주어지고. 각각 내림차순이다. 또한 정수 m 과 n이 주어지는데. m은 nums1 배열에서 병합되어야 하는 요소의 수 이고. 나머지 요소의 경우 0이다. n은 nums2 배열의 요소 수를 의미한다. 결과적으로 nums1에 nums2 배열을 합치는 문제이다.
내 풀이
:
nums2 요소 수가 0이 아닌 경우, (맞으면 병합할 요소가 없으므로 끝) nums1의 병합해야 하는 요소가 없으면 nums2 요소를 전부 nums1 배열에 넣는다. 병합해야할 요소가 있는 경우, nums1의 병합할 요소가 없어진 위치부터 nums2 요소를 넣는다. > 끝났으면 sort 함수를 이용해 정렬한다.
# Merge Sorted Array
# https://leetcode.com/explore/learn/card/fun-with-arrays/525/inserting-items-into-an-array/3253/
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.
"""
if n != 0:
if m == 0:
for i in range(0, n):
nums1[i] = nums2[i]
else:
for i in range(m, m + n):
nums1[i] = nums2[i - m]
nums1.sort()
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Check If N and Its Double Exist (0) | 2023.09.11 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Remove Element (0) | 2023.09.07 |
[파이썬 Python/알고리즘] LeetCode - Duplicate Zeros (0) | 2023.09.05 |
[파이썬 Python/알고리즘] LeetCode - Squares of a Sorted Array (0) | 2023.09.05 |
[파이썬 Python/알고리즘] LeetCode - Find Numbers with Even Number of Digits (0) | 2023.08.29 |