水题,没有在原数组上做,偷了个懒
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] ans = new int[m + n];
int i = 0, j = 0;
int top = 0;
while (i < m && j < n) {
if (nums1[i] < nums2[j]) {
ans[top++] = nums1[i++];
} else {
ans[top++] = nums2[j++];
}
}
while (i < m) {
ans[top++] = nums1[i++];
}
while (j < n) {
ans[top++] = nums2[j++];
}
for (int k = 0; k < m + n; k++) {
nums1[k] = ans[k];
}
}
}
原文地址:https://www.cnblogs.com/acbingo/p/9410748.html
时间: 2024-10-12 02:52:47