-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSortedArraySolution.java
51 lines (49 loc) · 1.28 KB
/
MergeSortedArraySolution.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
47
48
49
50
51
import java.util.Arrays;
class MergeSortedArraySolution {
/**
* @param A: sorted integer array A which has m elements,
* but size of A is m+n
* @param B: sorted integer array B which has n elements
* @return: void
*/
public static void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here
int[] S = new int[m + n];
int i = 0;
int j = 0;
int k = 0;
while(i < m && j < n ){
if(A[i] <= B[j]){
S[k] = A[i];
i++;
k++;
}
else{
S[k] = B[j];
j++;
k++;
}
}
System.out.println(k);
System.out.println(j);
while (i < m){
S[k] = A[i];
i++;
k++;
}
while (j < n){
S[k] = B[j];
j++;
k++;
}
System.out.println(Arrays.toString(S));
//A = S;
}
public static void main(String args[]){
int[] A = {1, 2, 3};
int m = A.length;
int[] B = {4,5};
int n = B.length;
mergeSortedArray(A,m,B,n);
}
}