Pergunta de entrevista da empresa Microsoft

merge two sorted arrays in sorted order?

Respostas da entrevista

Sigiloso

5 de mar. de 2014

public static int[] MergeSortedArray(int[] A1,int[] A2) { int A1Len=A1.Length;//Lenght of 1st array int A2Len = A2.Length;//Lenght of 2nd array if ((A1Len == 0) && (A2Len == 0)) return null; if (A1Len == 0) return A2; if (A2Len== 0) return A1; int[] A = new int[A1Len + A2Len];//output array int A1Index = 0;//start index of 1st array int A2Index = 0;//start index of 2nd array int index = 0;//start index of output array while (A1Index A2[A2Index]) { A[index] = A2[A2Index]; A2Index++; } else if(A1[A1Index] == A2[A2Index]) { A[index] = A2[A2Index]; A2Index++; A1Index++; } index++; } while (A1Index < A1Len) { A[index] = A1[A1Index]; A1Index++; index++; } while (A2Index < A2Len) { A[index] = A2[A2Index]; A2Index++; index++; } return A; }

Sigiloso

24 de mai. de 2020

public static int[] merge(int[] a,int[] b) { int m=0;int n=0; int k=a.length+b.length; int[] c=new int[k]; int j=0; while(m