题解 | #二维数组中的查找#

二维数组中的查找

https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e

C语言版本

bool Find(int target, int** array, int arrayRowLen, int* arrayColLen ) {

    // write code here

    int rowindex = 0;

    int colindex = 0;

    while(rowindex < arrayRowLen){      //当行索引没超过总行数则继续循环

        if(array[rowindex][*arrayColLen-1] < target){   //如果当前行的最后一个值(最大值)小于target,直接下一行

            rowindex++;

            continue;

        }

        if(array[rowindex][0] > target)     //如果当前行的第一个值(最小值)大于target,表示当前剩余矩阵不可能有target,退出

            return false;

       

        for(int i=0; i<(*arrayColLen); i++){    //遍历当前行

            if(array[rowindex][i] == target){   //找到就返回true

                return true;

            }

            if(array[rowindex][i] > target){    //当前值大于target,直接行+1,退出循环

                rowindex++;

                break;

            }

        }

    }

   

    return false;

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务