Skip to content

Commit 0620b38

Browse files
committed
0088. Merge Sorted Array
1 parent 1186791 commit 0620b38

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

markdown/0088. Merge Sorted Array.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
### [88\. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given two sorted integer arrays _nums1_ and _nums2_, merge _nums2_ into _nums1_ as one sorted array.
7+
8+
**Note:**
9+
10+
* The number of elements initialized in _nums1_ and _nums2_ are _m_ and _n_ respectively.
11+
* You may assume that _nums1_ has enough space (size that is greater or equal to _m_ + _n_) to hold additional elements from _nums2_.
12+
13+
**Example:**
14+
15+
```
16+
Input:
17+
nums1 = [1,2,3,0,0,0], m = 3
18+
nums2 = [2,5,6], n = 3
19+
20+
Output: [1,2,2,3,5,6]
21+
```
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
class Solution {
30+
   // public void merge(int[] nums1, int m, int[] nums2, int n) {
31+
   //     for (int i = m; i < m + n; i++) {
32+
   //         nums1[i] = nums2[i - m];
33+
   //     }
34+
   //     Arrays.sort(nums1);
35+
   // }
36+
37+
   public void merge(int[] nums1, int m, int[] nums2, int n) {
38+
       int x = m - 1;
39+
       int y = n - 1;
40+
       int i = m + n - 1;
41+
       while (x > -1 && y > -1) {
42+
           nums1[i--] = nums1[x] > nums2[y] ? nums1[x--] : nums2[y--];
43+
      }
44+
       while (y > -1) {
45+
           nums1[i--] = nums2[y--];
46+
      }
47+
  }
48+
}
49+
```
50+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-01-RSu1Dg.jpg)

src/main/java/leetcode/_88_/Main.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode._88_;
2+
3+
import leetcode.common.Printer;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
int[] nums1 = {1, 2, 3, 0, 0, 0};
12+
int[] nums2 = {2, 5, 6};
13+
solution.merge2(nums1, 3, nums2, 3);
14+
Printer.printArrays(nums1);
15+
}
16+
}
17+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode._88_;
2+
3+
import java.util.Arrays;
4+
5+
class Solution {
6+
public void merge(int[] nums1, int m, int[] nums2, int n) {
7+
for (int i = m; i < m + n; i++) {
8+
nums1[i] = nums2[i - m];
9+
}
10+
Arrays.sort(nums1);
11+
}
12+
13+
public void merge2(int[] nums1, int m, int[] nums2, int n) {
14+
int x = m - 1;
15+
int y = n - 1;
16+
int i = m + n - 1;
17+
while (x > -1 && y > -1) {
18+
nums1[i--] = nums1[x] > nums2[y] ? nums1[x--] : nums2[y--];
19+
}
20+
while (y > -1) {
21+
nums1[i--] = nums2[y--];
22+
}
23+
24+
}
25+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
### [88\. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given two sorted integer arrays _nums1_ and _nums2_, merge _nums2_ into _nums1_ as one sorted array.
7+
8+
**Note:**
9+
10+
* The number of elements initialized in _nums1_ and _nums2_ are _m_ and _n_ respectively.
11+
* You may assume that _nums1_ has enough space (size that is greater or equal to _m_ + _n_) to hold additional elements from _nums2_.
12+
13+
**Example:**
14+
15+
```
16+
Input:
17+
nums1 = [1,2,3,0,0,0], m = 3
18+
nums2 = [2,5,6], n = 3
19+
20+
Output: [1,2,2,3,5,6]
21+
```
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
class Solution {
30+
   // public void merge(int[] nums1, int m, int[] nums2, int n) {
31+
   //     for (int i = m; i < m + n; i++) {
32+
   //         nums1[i] = nums2[i - m];
33+
   //     }
34+
   //     Arrays.sort(nums1);
35+
   // }
36+
37+
   public void merge(int[] nums1, int m, int[] nums2, int n) {
38+
       int x = m - 1;
39+
       int y = n - 1;
40+
       int i = m + n - 1;
41+
       while (x > -1 && y > -1) {
42+
           nums1[i--] = nums1[x] > nums2[y] ? nums1[x--] : nums2[y--];
43+
      }
44+
       while (y > -1) {
45+
           nums1[i--] = nums2[y--];
46+
      }
47+
  }
48+
}
49+
```
50+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-08-01-RSu1Dg.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy