Skip to content

Commit fa88219

Browse files
authored
88.Merge-双指针.java
1 parent 9ff3039 commit fa88219

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Merge.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public void merge(int[] nums1, int m, int[] nums2, int n) {
3+
int l = m - 1;
4+
int r = n - 1;
5+
int s = m + n - 1;
6+
while (l >= 0 && r >= 0) {
7+
if (nums1[l] >= nums2[r]) {
8+
nums1[s] = nums1[l];
9+
l--;
10+
} else {
11+
nums1[s] = nums2[r];
12+
r--;
13+
}
14+
s--;
15+
}
16+
if (l < 0) {
17+
while (r >= 0) {
18+
nums1[s] = nums2[r];
19+
r--;
20+
s--;
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)