diff --git a/0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md b/0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md index 7ae9e51..602e3b5 100644 --- a/0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md +++ b/0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md @@ -77,6 +77,94 @@ nums2 = [2,5,6], n = 3 Animation ### 参考代码 +C++ Code: + +```c++ +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + int i=m-1, j=n-1, k=m+n-1; + // 合并 + while(i>=0 && j>=0) + { + if(nums1[i] > nums2[j]) + { + nums1[k--] = nums1[i--]; + } + else + { + nums1[k--] = nums2[j--]; + } + } + // 合并剩余的nums2 + while(j>=0) + { + nums1[k--] = nums2[j--]; + } + } +}; +``` + +Java Code: + +```java +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int i=m-1, j=n-1, k=m+n-1; + // 合并 + while(i>=0 && j>=0) + { + if(nums1[i] > nums2[j]) + { + nums1[k--] = nums1[i--]; + } + else + { + nums1[k--] = nums2[j--]; + } + } + // 合并剩余的nums2 + while(j>=0) + { + nums1[k--] = nums2[j--]; + } + } +} +``` + +Python Code: + +```python +class Solution(object): + def merge(self, nums1, m, nums2, n): + """ + :type nums1: List[int] + :type m: int + :type nums2: List[int] + :type n: int + :rtype: None Do not return anything, modify nums1 in-place instead. + """ + i,j,k = m-1, n-1, m+n-1 + + while i >= 0 and j >= 0: + # print(i,j,k, nums1) + # print(nums1[i], nums2[j]) + if nums1[i] > nums2[j]: + nums1[k] = nums1[i] + k-=1 + i-=1 + else: + nums1[k] = nums2[j] + k-=1 + j-=1 + while j >= 0: + nums1[k] = nums2[j] + k-=1 + j-=1 + +``` + +JavaScript Code: ```javascript /** @@ -105,4 +193,4 @@ var merge = function(nums1, m, nums2, n) { -![](../../Pictures/qrcode.jpg) \ No newline at end of file +![](../../Pictures/qrcode.jpg)