Skip to content

Commit 56dab40

Browse files
authored
88.Merge-双指针
1 parent 5ce463d commit 56dab40

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

Merge.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ public void merge(int[] nums1, int m, int[] nums2, int n) {
33
int l = m - 1;
44
int r = n - 1;
55
int s = m + n - 1;
6+
// 从后往前遍历
67
while (l >= 0 && r >= 0) {
78
if (nums1[l] >= nums2[r]) {
89
nums1[s] = nums1[l];
@@ -13,6 +14,7 @@ public void merge(int[] nums1, int m, int[] nums2, int n) {
1314
}
1415
s--;
1516
}
17+
// 如果nums1已遍历完而nums2还有数,则将剩余数复制到nums1;若nums1还有数则不需要动
1618
if (l < 0) {
1719
while (r >= 0) {
1820
nums1[s] = nums2[r];

0 commit comments

Comments
 (0)