public int[] mergeArrays(int[] myArray, int[] alicesArray) {
// 조건 myArray 와 alicesArray둘다 각각 이미 정렬되어 있어야한다.
int[] mergedArray = new int[myArray.length + alicesArray.length];
int currentIndexAlices = 0;
int currentIndexMine = 0;
int currentIndexMerged = 0;
while (currentIndexMerged < mergedArray.length) {
boolean isMyArrayExhausted = currentIndexMine >= myArray.length;
boolean isAlicesArrayExhausted = currentIndexAlices >= alicesArray.length;
if (!isMyArrayExhausted && (isAlicesArrayExhausted
|| (myArray[currentIndexMine] < alicesArray[currentIndexAlices]))) {
mergedArray[currentIndexMerged] = myArray[currentIndexMine];
currentIndexMine++;
} else {
mergedArray[currentIndexMerged] = alicesArray[currentIndexAlices];
currentIndexAlices++;
}
currentIndexMerged++;
}
return mergedArray;
}
댓글
댓글 쓰기