Created
May 25, 2016 18:53
-
-
Save jianminchen/0acddf70788fce10b9660dc16b2e3b71 to your computer and use it in GitHub Desktop.
Leetcode 88 - Merge Two sorted Array
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace LC88_MergeTwoSortedArray | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
} | |
/* | |
* From end to beginning, | |
* | |
*/ | |
void merge(int[] A, int m, int[] B, int n) { | |
int endIndex = m+n-1; | |
int endA = m-1, | |
endB =n-1; | |
for(; endA>=0 && endB>=0; endIndex--) | |
{ | |
if(A[endA] >= B[endB]) | |
{ | |
A[endIndex] = A[endA]; | |
endA--; | |
} | |
else | |
{ | |
A[endIndex] = B[endB]; | |
endB--; | |
} | |
} | |
// If there are some leftover in array B, copy all over; | |
// else if there are some leftover in array A, leave as is | |
while(endB >=0) | |
{ | |
A[endIndex] = B[endB]; | |
endIndex--; | |
endB--; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment