-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortedArray_88.java
More file actions
34 lines (29 loc) · 899 Bytes
/
MergeSortedArray_88.java
File metadata and controls
34 lines (29 loc) · 899 Bytes
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
/**
* 1. This is easy but easily make things complicated!!
* Move A's element to the end of A
* Merge A & B
*/
/*public class Solution {
public void merge(int A[], int m, int B[], int n) {
for(int i=m-1; i>=0; i--) { A[i+n] = A[i]; }
int index = 0, i = n, j = 0;
while(i<m+n && j<n)
A[index++] = (B[j] <= A[i])? B[j++] : A[i++];
while (j < n)
A[index++] = B[j++];
// do not need to copy A's remaining.
}
}*/
/**
* Do not need to copy at all, we can do it in the oppsite way
* Put the largest to the end!
*/
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int index = m+n-1, i = m-1, j = n-1;
while(i>=0 && j>=0)
A[index--] = B[j] >= A[i] ? B[j--] : A[i--];
while (j>=0)
A[index--] = B[j--];
}
}