[Leetcode] 88. Merge Sorted Array
[Leetcode] 88. Merge Sorted Array

[Leetcode] 88. Merge Sorted Array

์–ธ์–ด
Python
C
๋‚œ์ด๋„
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•
์ž‘์„ฑ์ž
๋ฐ•์šฉ์„ฑ๋ฐ•์šฉ์„ฑ
์ƒ์„ฑ ์ผ์‹œ
2024๋…„ 09์›” 03์ผ
floatFirstTOC: right

๐Ÿ–ฅ๏ธย ์‹œ์ž‘ํ•˜๋ฉฐ

๋ฌธ์ œ ์กฐ๊ฑด์„ ์‚ดํŽด๋ดค์„ ๋•Œ, ํŒŒ์ด์ฌ์€ ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ๋กœ, C++์€ STL๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
์˜ˆ์ œ 1๋ฒˆ์—์„œ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ๋ณด๋ฉด [1,2,3,0,0,0], m = 3 ์œผ๋กœ, ๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ 3์ด๋‹ˆ ๋’ค์˜ 0์€ ๊ทธ๋ƒฅ ๋ฌด์‹œํ•ด๋ฒ„๋ ค๋„ ๋ฌด๋ฐฉํ•˜๋‹ค. ์ฆ‰ ๋ฐฐ์—ด ํฌ๊ธฐ๋งŒํผ ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ del ์„ ์ด์šฉํ•ด ๋ฐฐ์—ด์„ ์ •์ œํ•ด์ค€ ๋‹ค์Œ ํ•ฉ์น˜๊ณ  ์ •๋ ฌํ•˜๋ฉด ๋œ๋‹ค.
ย 

โš™๏ธย Python

from typing import List 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. """ del nums1[m:] del nums2[n:] nums1.extend(nums2) nums1.sort() if __name__ == "__main__": sol = Solution() print( sol.merge(nums1=[1, 2, 3, 0, 0, 0], m=3, nums2=[2, 5, 6], n=3) ) # [1, 2, 2, 3, 5, 6]
ย 

๐Ÿ“Œย ์†Œ๊ฐ

๋Œ“๊ธ€

guest