// 二维数组查找

// 二维数组查找



#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;
}

posted on 2020-04-19 14:14  心中日月007  阅读(105)  评论(0编辑  收藏  举报