题目描述

请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征:
每一行的数字都从左到右排序
每一行的第一个数字都比上一行最后一个数字大
例如:
对于下面的矩阵:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

要搜索的目标值为3,返回true;


Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
    [1,   3,  5,  7],
    [10, 11, 16, 20],
    [23, 30, 34, 50]
]

Given target =3, returntrue.


示例1

输入

复制

[[1,1]],0

输出

复制

false


class Solution {
public:
    /**
     *
     * @param matrix int整型vector<vector<>>
     * @param target int整型
     * @return bool布尔型
     */
    bool searchMatrix(vector<vector<int> >& matrix, int target) {
        // write code here
        int rows=matrix.size();
        int cols=matrix[0].size();
        int begin=0,end=rows*cols-1;
        
        while (begin <= end)
        {
            int mid=(begin+end)/2;
            int row=mid/cols;
            int col=mid%cols;
            if (matrix[row][col] ==target)
                return true;
            else if (matrix[row][col]<target)
                begin=mid+1;
            else
                end=mid-1;
            
        }
        return false;
    }
};

leetcode75:search-a-2d-matrix的更多相关文章

  1. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  2. [LeetCode] Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. 【leetcode】Search a 2D Matrix

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  4. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  5. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  6. Search a 2D Matrix | & II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...

  7. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  8. LintCode 38. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...

  9. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  10. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

随机推荐

  1. 【Office-Word妙手回春】Word文本秒转表格

    第一步:Ctrl+A组合键,文本全选 第二步:插入→表格→文本转换成表格 第三步:在"文字分隔位置",勾选相应的符号. 此处的分隔符为 空格. 点击"确定"按钮 ...

  2. Oracle 数据库创建表空间、创建用户

    创建表空间 create temporary tablespace user_name_temp tempfile '/oradata/ORA11G/user_name_temp.dbf' size ...

  3. ansible-playbook-roles基本使用

    1. ansible-角色-roles基本使用  1.1) 创建roles目录结构 1 [root@test-1 ansible]# mkdir -p /ansible/roles/{common,n ...

  4. js 为什么0.1+0.2不等于0.3

    当程序员在使用浮点数进行计算逻辑处理时,不注意,就可能出现问题, 记住,永远不要直接比较俩个浮点的大小 这个属于数字运算中的精度缺失的问题 在0.1 + 0.2这个式子中,0.1和0.2都是近似表示的 ...

  5. Js中Currying的应用

    Js中Currying的应用 柯里化Currying是把接受多个参数的函数变换成接受一个单一参数的函数,并且返回接受余下的参数且返回结果的新函数的技术,是函数式编程应用. 描述 如果说函数式编程中有两 ...

  6. pytest文档40-pytest.ini配置用例查找规则(面试题)

    前言 面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例. pytest.ini 配置文件可以修改用例的匹配规则. pytest命令行参数 cmd打开输入pyt ...

  7. Monolog - Logging for PHP

    github地址:https://github.com/Seldaek/monolog 使用 Monolog 安装 核心概念 日志级别 配置一个日志服务 为记录添加额外的数据 使用通道 自定义日志格式 ...

  8. selenium-窗口切换

    方法一 # 获取打开的多个窗口句柄 windows = driver.window_handles # 切换到当前最新打开的窗口 driver.switch_to.window(windows[-1] ...

  9. js 实现 input file 文件上传

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  10. Linux中创建自己的欢迎登陆界面

    /etc 在Linux中相当于Windows的注册表 修改其中文件可以影响整个Linux系统 MOTD motd:message of the day /etc/motd /etc/motd文件作用是 ...