-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSortedArray.java
46 lines (43 loc) · 1.07 KB
/
MergeSortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by JiahengYu on 08/06/15.
*/
public class MergeSortedArray {
public void merge(int[] nums1, int m, int[] nums2, int n) {
if (nums2==null||nums2.length==0)
return;
int one=m-1;
int two=n-1;
int current=nums1.length-1;
while (one>=0&&two>=0){
if (nums1[one]>=nums2[two]){
nums1[current]=nums1[one];
one--;
}else {
nums1[current]=nums2[two];
two--;
}
current--;
}
while (one>=0){
nums1[current]=nums1[one];
one--;
current--;
}
while (two>=0){
nums1[current]=nums2[two];
two--;
current--;
}
}
public static void main(String args[]){
MergeSortedArray test=new MergeSortedArray();
int a[]={1,0};
int b[]={2};
test.merge(a,1,b,1);
for (Integer c:a){
System.out.print(c);
}
}
}