Last active
March 16, 2023 04:54
-
-
Save VallarasuS/65a67b942210fb8127ce48e6104f0bfa to your computer and use it in GitHub Desktop.
88. Merge Sorted Array - https://leetcode.com/problems/merge-sorted-array/description/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public void merge(int[] nums1, int m, int[] nums2, int n) { | |
int capacity = m + n; | |
for(int i = capacity -1; i >= 0; i--) { | |
if(n < 1) { | |
return; | |
} | |
if(m < 1) { | |
nums1[i] = nums2[--n]; | |
continue; | |
} | |
if(nums1[m - 1] > nums2[n - 1]) { | |
nums1[i] = nums1[--m]; | |
} | |
else { | |
nums1[i] = nums2[--n]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment