Skip to content

Commit 24262ca

Browse files
committedJul 27, 2024
88 done
1 parent 76fd125 commit 24262ca

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
 

‎88_Merge Sorted Array/solution.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 88. Merge Sorted Array
2+
# You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing
3+
# the number of elements in nums1 and nums2 respectively.
4+
#
5+
# Merge nums1 and nums2 into a single array sorted in non-decreasing order.
6+
#
7+
# The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate
8+
# 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
9+
# set to 0 and should be ignored. nums2 has a length of n.
10+
#
11+
12+
# class Solution:
13+
# def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
14+
# """
15+
# Do not return anything, modify nums1 in-place instead.
16+
# """
17+
#
18+
#
19+
#
20+
#
21+
# Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
22+
# Output: [1,2,2,3,5,6]
23+
# Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
24+
# The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
25+
#
26+
27+
28+
# def sortedarr(nums1, nums2, m, n):
29+
# nums1 += nums2
30+
# nums1.sort(reverse=True)
31+
# number = m + n
32+
# nums3 = nums1[0:number]
33+
# nums1 = nums3
34+
# nums1.sort()
35+
# print(nums1)
36+
#
37+
#
38+
#
39+
# nums1, nums2, m, n = [1,2,3,0,0,0], [2,5,6], 3, 3
40+
#
41+
# sortedarr
42+
43+
# 123000
44+
# 256
45+
46+
#
47+
# def sorted(nums1, nums2, m, n):
48+
# pointer1 = m - 1
49+
# pointer2 = n - 1
50+
# pointer = m + n - 1
51+
#
52+
# while pointer2 >= 0:
53+
# if nums2[pointer2] > nums1[pointer1]:
54+
# nums1[pointer] = nums2[pointer2]
55+
# pointer2 -= 1
56+
# elif nums1[pointer1] >= nums2[pointer2]:
57+
# nums1[pointer] = nums1[pointer1]
58+
# pointer1 -= 1
59+
# else:
60+
# nums1[pointer] = nums2[pointer2]
61+
# pointer2 -= 1
62+
# pointer -= 1
63+
#
64+
# print(nums1)
65+
#
66+
#
67+
# nums1, nums2, m, n = [1, 2, 3, 0, 0, 0], [2, 5, 6], 3, 3
68+
#
69+
# sorted(nums1, nums2, m, n)
70+
71+
72+
### leetcode with class final solution
73+
74+
class Solution:
75+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
76+
"""
77+
Do not return anything, modify nums1 in-place instead.
78+
"""
79+
pointer1 = m - 1
80+
pointer2 = n - 1
81+
pointer = m + n - 1
82+
83+
while pointer2 >= 0:
84+
if nums2[pointer2] > nums1[pointer1] and pointer1 > 0:
85+
nums1[pointer] = nums2[pointer2]
86+
pointer2 -= 1
87+
elif nums1[pointer1] >= nums2[pointer2] and pointer1 >= 0:
88+
nums1[pointer] = nums1[pointer1]
89+
pointer1 -= 1
90+
else:
91+
nums1[pointer] = nums2[pointer2]
92+
pointer2 -= 1
93+
pointer -= 1

0 commit comments

Comments
 (0)
Please sign in to comment.