• // 二维数组查找


    // 二维数组查找



    #include "stdafx.h"
    using namespace std;
    #include <string>
    #include <vector>

    class Solution {
    public:
        bool Find(int target, vector<vector<int> > array) {
            int rows = array.size();
            int cols = array[0].size();
            if (!array.empty() && rows > 0 && cols > 0)
            {
                int row = 0;
                int col = cols - 1;
                while (row < rows && col >= 0)
                {
                    if (array[row][col] > target)
                    {
                        col = col - 1;
                    }
                    else if (array[row][col] < target)
                    {
                        row = row + 1;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    };

    int main()
    {
        int array[4][4] = {
                            { 1, 2, 8, 9 },
                            { 2, 4, 9, 12 },
                            { 4, 7, 10, 13 },
                            {6,8,11,15} ,
                        };
        vector<int> aa = { 1, 2, 8, 9 };
        vector<int> bb = { 2, 4, 9, 12 };
        vector<int> cc = { 4, 7, 10, 13 };
        vector<int>dd = { 6, 8, 11, 15 };

        vector<vector<int>> kk = { aa, bb, cc, dd };
        Solution sou;
        sou.Find(7, kk);
        return 1;
    }

    天天向上
  • 相关阅读:
    tcpip协议
    Linux特殊文件使用原理
    shell之常用内置变量
    SSM框架CRUD小案例
    个人博客开发笔记
    蓝天筹项目开发记录
    二叉平衡树AVL的插入与删除(java实现)
    二叉搜索树(java实现)
    树的基本操作
    SQL连接
  • 原文地址:https://www.cnblogs.com/hg07/p/12731408.html
Copyright © 2020-2023  润新知