开头啰嗦两句。刚在做算法题目的时候。听到了杨宗纬的《初爱》,突然有了一种本科时候的感觉,想想自己如今研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐…..
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
if(n == 0)return;
int all = m+n-1;
m--;
n--;
while(m >=0 && n >=0){
nums1[all--] = nums1[m] > nums2[n] ? nums1[m--]:nums2[n--];
}
while(n >= 0){
nums1[all--] = nums2[n--];
}
}
}
LeetCode(40)-Merge Sorted Array
原文:https://www.cnblogs.com/llguanli/p/8947425.html