Skip to content

Commit 4451f61

Browse files
committed
refine
1 parent 5337b3d commit 4451f61

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

MergeSortedArray.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
/**
42
* Given two sorted integer arrays A and B, merge B into A as one sorted array.
53
*
@@ -11,18 +9,13 @@
119

1210
public class MergeSortedArray {
1311
public void merge(int A[], int m, int B[], int n) {
14-
int end = m + n - 1;
15-
int a = m - 1;
16-
int b = n - 1;
17-
while (a >= 0 && b >= 0) {
18-
if (A[a] >= B[b]) {
19-
A[end--] = A[a--];
20-
} else {
21-
A[end--] = B[b--];
22-
}
12+
int i = m - 1, j = n - 1;
13+
int k = m + n - 1;
14+
while (i >= 0 && j >= 0) {
15+
A[k--] = A[i] >= B[j] ? A[i--] : B[j--];
2316
}
24-
while (b >= 0) {
25-
A[end--] = B[b--];
17+
while (j >= 0) {
18+
A[k--] = B[j--];
2619
}
2720
}
2821
}

0 commit comments

Comments
 (0)