-
2021-01-12] Merge Sorted ArrayIT/자기계발 ( Leetcode ) 2021. 1. 12. 00:00반응형
오늘의 문제:
간만쓰 쉬운 문제였던것 같다..ㅎㅎ 주말엔 시간이 많을테니 어려운 문제를 주는지 토/일은 문제가 힘들어서 고통이었다ㅜ
이 문제는 array관련 문제로 예시를 보며 이해해보자.
<예시 1-2> nums1의 길이는 두 list를 합칠만큼의 공간이 주어지고, 실제 값이 있는 길이는 m으로 주어진다. 이를 sort하여 값을 주면 완성.
주의할 점은 return값이 없고 주어진 nums1 list에 sort된 값을 넣어서 반환하면 된다.
문제풀이)
class Solution(object): def merge(self, nums1, m, nums2, n): """ :type nums1: List[int] :type m: int :type nums2: List[int] :type n: int :rtype: None Do not return anything, modify nums1 in-place instead. """ ans=[] for i in range(len(nums1)): if i< m: ans.append(nums1[i]) # m보다 작으면 nums1에서 데이터 삽입 else: ans.append(nums2[i-m]) # m보다 크거나 같으면 nums2에서 데이터 삽입 ans=sorted(ans) # 정렬 for i in range(len(nums1)): #정렬된 값을 nums1에 삽입 nums1[i]=ans[i]
반응형'IT > 자기계발 ( Leetcode )' 카테고리의 다른 글
2021-01-14] Boats to Save People (0) 2021.01.14 2021-01-13] Add Two Numbers (0) 2021.01.13 2021-01-11] Valid Sudoku (0) 2021.01.11 2021-01-10] Create Sorted Array through Instructions (0) 2021.01.11 2021-01-09] Check If Two String Arrays are Equivalent & Duplicate Zeros (0) 2021.01.09