zoukankan      html  css  js  c++  java
  • 《剑指offer》第四题:二维数组中的查找

    // 面试题4:二维数组中的查找
    // 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按
    // 照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个
    // 整数,判断数组中是否含有该整数。
    
    #include <cstdio>
    
    bool Find(int* matrix, int rows, int columns, int number)
    {
        //鲁棒性测试 1.空指针 2.矩阵大小为0 3.number小于0
        if (matrix == nullptr || rows <= 0 || columns <= 0 || number <= 0)
            return false;
    
        //主要思路:从右上角数开始对比,如小于右上角数则列数-1,如大于则行数-1
        int cur_row = 0;
        int cur_col = columns - 1;
    
        while (cur_row < rows && cur_col >= 0) //未找到则跳出
        {    
            if (matrix[cur_row * columns + cur_col] == number) //右上角数正好为该数
                return true;
            else if (matrix[cur_row * columns + cur_col] > number) //小于该列最小值,列数-1
                --cur_col;
            else //大于该行最大值,行数-1
                ++cur_row;
        }
        return false;
    }
    // ====================测试代码====================
    void Test(const char* testName, int* matrix, int rows, int columns, int number, bool expected)
    {
        if (testName != nullptr)
            printf("%s begins: ", testName);
    
        bool result = Find(matrix, rows, columns, number);
        if (result == expected)
            printf("Passed.
    ");
        else
            printf("Failed.
    ");
    }
    
    //  1   2   8   9
    //  2   4   9   12
    //  4   7   10  13
    //  6   8   11  15
    // 要查找的数在数组中
    void Test1()
    {
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        Test("Test1", (int*)matrix, 4, 4, 7, true);
    }
    
    //  1   2   8   9
    //  2   4   9   12
    //  4   7   10  13
    //  6   8   11  15
    // 要查找的数不在数组中
    void Test2()
    {
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        Test("Test2", (int*)matrix, 4, 4, 5, false);
    }
    
    //  1   2   8   9
    //  2   4   9   12
    //  4   7   10  13
    //  6   8   11  15
    // 要查找的数是数组中最小的数字
    void Test3()
    {
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        Test("Test3", (int*)matrix, 4, 4, 1, true);
    }
    
    //  1   2   8   9
    //  2   4   9   12
    //  4   7   10  13
    //  6   8   11  15
    // 要查找的数是数组中最大的数字
    void Test4()
    {
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        Test("Test4", (int*)matrix, 4, 4, 15, true);
    }
    
    //  1   2   8   9
    //  2   4   9   12
    //  4   7   10  13
    //  6   8   11  15
    // 要查找的数比数组中最小的数字还小
    void Test5()
    {
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        Test("Test5", (int*)matrix, 4, 4, 0, false);
    }
    
    //  1   2   8   9
    //  2   4   9   12
    //  4   7   10  13
    //  6   8   11  15
    // 要查找的数比数组中最大的数字还大
    void Test6()
    {
        int matrix[][4] = { {1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15} };
        Test("Test6", (int*)matrix, 4, 4, 16, false);
    }
    
    // 鲁棒性测试,输入空指针
    void Test7()
    {
        Test("Test7", nullptr, 0, 0, 16, false);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        Test7();
    
        return 0;
    }
    测试代码

    分析:用实例分析过程,找到规律。

    牛客网代码模板给出了vector,可以通过array[row][col]直接访问。

    class Solution {
    public:
        bool Find(int target, vector<vector<int> > array) {
            
            if (array.empty() || target <= 0)
                return false;
            
            int cur_row = 0;
            int cur_col = array[0].size()-1;
            
            while (cur_row < array[0].size() && cur_col >= 0)
            {
                if (array[cur_row][cur_col] == target)
                    return true;
                else if (array[cur_row][cur_col] > target)
                    --cur_col;
                else
                    ++cur_row;
            }
            return false;
            
        }
    };
    牛客网提交代码
  • 相关阅读:
    【vue知识点】2)vue登录认证
    【vue知识点】1)vue生命周期
    【VSC】.txt文件打开乱码
    【VSC】git+github/码云+VSCode
    【vsCode】我安装了哪些扩展插件
    【VCS】种草VSCode
    【vue】父子组件间通信----传函数
    最大子列和
    jedisLock—redis分布式锁实现
    基础的重要性(程序员之路)
  • 原文地址:https://www.cnblogs.com/ZSY-blog/p/12500682.html
Copyright © 2011-2022 走看看