Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created May 25, 2016 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/0acddf70788fce10b9660dc16b2e3b71 to your computer and use it in GitHub Desktop.
Save jianminchen/0acddf70788fce10b9660dc16b2e3b71 to your computer and use it in GitHub Desktop.
Leetcode 88 - Merge Two sorted Array
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