二维数组的查找 剑指offer

题目描述

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
输入描述:
array: 待查找的二维数组
target:查找的数字
输出描述:
查找到返回true,查找不到返回false

bool Find(vector<vector<int> > array,int target) {
        bool found = false;
        int rows = array.size();
        int columns = array[0].size();
        if(!array.empty() && rows > 0 && columns > 0){
            int row=0;
            int column = columns-1;
            while(row < rows && column >= 0){
                if(array[row][column] == target){
                    found = true;
                    break;
                }
                else if(array[row][column] > target)
                    --column;
                else
                    ++row;
            }
        }
        return found;
    }
转载请注明出处: C++博客园:godfrey_88 http://www.cnblogs.com/gaobaoru-articles/
原文地址:https://www.cnblogs.com/gaobaoru-articles/p/5235248.html