https://leetcode.com/problems/merge-sorted-array/
2個排序好的陣列,合併成一個排序好的陣列。
從2個陣列的最後一個值互相判斷大小,將得到的值放入第一個陣列最後一個,直到條件比完為止。
void merge(int* nums1, int m, int* nums2, int n) {
while(m>0 && n>0)
{
if(nums1[m-1] > nums2[n-1])
{
nums1[m+n-1] = nums1[m-1];
m--;
}
else
{
nums1[m+n-1] = nums2[n-1];
n--;
}
}
while(n>0)
{
nums1[m+n-1] = nums2[n-1];
n--;
}
while(m>0)
{
nums1[m+n-1] = nums1[m-1];
m--;
}
}
var merge = function(nums1, m, nums2, n) {
var len = m + n;
m--;
n--;
while (len--) {
if (n < 0 || nums1[m] > nums2[n]) {
nums1[len] = nums1[m--];
} else {
nums1[len] = nums2[n--];
}
}
};
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
We are tougher than we think; one day when you look back, you’ll realize that you’ve walked a path you’ve never thought you would.
人遠比想像的要堅強,當有天你回頭看的時候你會發現你走了一段自己都沒想過的路