题目描述
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
时间限制:1秒 空间限制:32768K 热度指数:315456
思路:将二维数组当成矩阵,从右上角出发,比较目标数字与当前数字的大小。如果目标数字小于当前数字,则目标数字所处的列都不可能出现目标数字。(因为每一列按照从上往下递增);如果目标数字大于当前数字,则当前数字所处的行都不可能出现目标数字。(因为每一行按照从左往右递增)。按照这样的步骤,可以缩小查找的范围,直到找到要查找的数字,或者不存在目标数字。
参考代码:
C++版本: