【剑指Offer学习】【面试题3 :二维数组中的查找】

https://blog.csdn.net/derrantcm/article/details/45330789

bool hasNum(int **num, int row, int col, int finder) {
    if(NULL == num || 0 == row || 0 == col) {
        return  false;
    }
    int curRow = 0;
    int curCol = col-1;
    while (curCol >=0 && curRow < row) {
        if(num[curRow][curCol] == finder) {
            return true;
        }
        else if(num[curRow][curCol] > finder) {
            curCol--;
        }
        else {
            curRow++;
        }
    }
    return false;
}



    int *matrix[] =
    {
        (int[]){  1,  4,  7, 11, 15 },
        (int[]){  2,  5,  8, 12, 19 },
        (int[]){  7,  8,  9, 16, 22 },
        (int[]){ 10, 13, 14, 17, 24 },
        (int[]){ 18, 21, 23, 26, 30 },
    };
    
    bool has = hasNum(matrix,5,5,3);
    bool has1 = hasNum(matrix,5,5,6);
    bool has2 = hasNum(matrix,5,5,11);
发布了81 篇原创文章 · 获赞 68 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/li198847/article/details/104383756