Skip to content

Commit 1a1e378

Browse files
committedMay 30, 2018
88.merge-sorted-array
1 parent b5b2447 commit 1a1e378

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This is a **continually updated, open source** project.
1111

1212
- [Leetcode](#leetcode)
1313
- [Algorithms](#algorithms)
14+
- [Array](#array)
1415
- [Two Pointers](#two-pointers)
1516
- [Linked List](#linked-list)
1617
- [Binary Search](#binary-search)
@@ -21,6 +22,12 @@ This is a **continually updated, open source** project.
2122
- [Topological Sort](#topological-sort)
2223
- [Dynamic Programming](#dynamic-programming)
2324

25+
## Array
26+
27+
| Problem | Solution | Time | Difficulty | Tag | Note|
28+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
29+
| 88.merge-sorted-array | [python](./algorithm/88.merge-sorted-array.py) | O(N + M) | Easy |
30+
2431
## Two Pointers
2532

2633
| Problem | Solution | Time | Difficulty | Tag | Note|

‎algorithm/88.merge-sorted-array.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def merge(self, nums1, m, nums2, n):
3+
"""
4+
:type nums1: List[int]
5+
:type m: int
6+
:type nums2: List[int]
7+
:type n: int
8+
:rtype: void Do not return anything, modify nums1 in-place instead.
9+
"""
10+
11+
left = m - 1
12+
right = n - 1
13+
total = m + n - 1
14+
15+
while (left >= 0 and right >= 0):
16+
if nums1[left] < nums2[right]:
17+
nums1[total] = nums2[right]
18+
right -= 1
19+
else:
20+
nums1[total] = nums1[left]
21+
left -= 1
22+
total -= 1
23+
24+
while (right >= 0):
25+
nums1[total] = nums2[right]
26+
right -= 1
27+
total -= 1

0 commit comments

Comments
 (0)
Please sign in to comment.