74. 搜索二维矩阵

74. 搜索二维矩阵

已解答

中等

相关标签

相关企业

给你一个满足下述两条属性的 m x n 整数矩阵:

  • 每行中的整数从左到右按非严格递增顺序排列。
  • 每行的第一个整数大于前一行的最后一个整数。

给你一个整数 target ,如果 target 在矩阵中,返回 true ;否则,返回 false 。

示例 1:

复制代码
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:true

示例 2:

复制代码
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
输出:false

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -104 <= matrix[i][j], target <= 104
cpp 复制代码
class Solution {
public:
    int row, col, len;

    bool bSearch(vector<vector<int>>& matrix, int target, int start, int end){
        if(end - start <= 1){
            return matrix[start / col][start % col] == target;
        }
        int mid = (start + end) / 2;
        if(matrix[mid /col][mid % col] > target){
            return bSearch(matrix, target, start, mid);
        }
        else{
            return bSearch(matrix, target, mid, end);
        }
    }


    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        row = matrix.size();
        col = matrix[0].size();
        len = row * col;
        return bSearch(matrix, target, 0, len);
    }
};
相关推荐
老师我太想进步了20266 小时前
【数据结构-15】实现链式结构二叉树
c语言·数据结构
liulilittle6 小时前
认识麻将 —— 流水及对账
算法·游戏·lua·对账·流水·资金·玩法
xiangxueCode8 小时前
数据结构:容器适配器之优先级队列
数据结构·优先级队列
鱼很腾apoc9 小时前
【Redis】第2期 详解Redis五大数据结构:命令+C++实现、渐进式遍历与数据库管理
linux·服务器·c语言·数据结构·c++·redis·学习
郝学胜-神的一滴10 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
青少儿编程课堂1 天前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
geovindu1 天前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗1 天前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
路灯中转站1 天前
数据结构C语言版(什么是数据类型)
数据结构