百度外卖面试题
新浪面试题
都考了这个(归并排序)
合并两个有序数组
/**
* 两个有序数据 合并
* @author xpisme <gxpisme@gmail.com>
* @date 2016-12-12
* @return array [description]
*/
function getSortArr($arrOne, $arrTwo)
{
$oneLength = count($arrOne);
$twoLength = count($arrTwo);
$res = [];
$oneIndex = 0;
$twoIndex = 0;
// 1 循环两个数组
while ( ($oneIndex < $oneLength) && ($twoIndex < $twoLength) ) {
$res[] = $arrOne[$oneIndex] > $arrTwo[$twoIndex] ? $arrTwo[$twoIndex++] : $arrOne[$oneIndex++];
}
// 2 将剩余的oneArr中 添加到res数组
while ($oneIndex < $oneLength) {
$res[] = $arrOne[$oneIndex++];
}
// 3 将剩余的twoArr中 添加到res数组
while ($twoIndex < $twoLength) {
$res[] = $arrTwo[$twoIndex++];
}
return $res;
}