排序_在单调矩阵中查找值

矩阵$arr, 查找是否包含目标值$tg

function a($arr, $n, $tg)
{
    $i = 0;
    $j = $n;
    while (true) {
        if ($tg == $arr[$i][$j]) {
            return "找到了" . $i . '--' . $j;
        } else if ($tg > $arr[$i][$j]) {
            ++$i;
        } else {
            --$j;
        }
        if ($i > $n || $j < 0) { // 下标都越界了,肯定是没有
            return "没有找到";
        }

    }
}


$arr = [
    [1, 3, 5],
    [2, 5, 7],
    [4, 6, 10],
];


$tg = 100;
$b = a($arr, 2, $tg);
echo $b;

posted @ 2017-03-27 11:08  Thomas_188  阅读(191)  评论(0编辑  收藏  举报