1. Search a 2D Matrix II

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 in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

Example:

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

从右上角到左下角,往左搜索变小,往下搜索变大,假设在位置 i, j处的元素值为n,搜索过程为:

  • target == n :找到,结束
  • target > n:往下走,增加n
  • target < n:往左走,降低n
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if(m == 0)return false;
int n = matrix[0].size();
int i = 0, j = n-1;
while(i < m && j >= 0){
if(matrix[i][j] == target)return true;
if(matrix[i][j] > target){
j--;
}else{
i++;
}
}
return false;
}
};

【刷题-LeetCode】240. Search a 2D Matrix II的更多相关文章

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

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

  2. (medium)LeetCode 240.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 ...

  3. Leetcode 240. 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 ...

  4. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

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

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

  6. 【LeetCode】240. 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. Thi ...

  7. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【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 ...

  9. [LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search

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

随机推荐

  1. LuoguP7398 [COCI2020-2021#5] Šifra 题解

    Content 给定一个长度 \(n\) 的只包含小写字母和 \(0\sim9\) 的字符串(字符串中的字母可视为分隔符).求字符串中包含多少个不同的数字. 数据范围:\(1\leqslant n\l ...

  2. 使用Redis+自定义注解实现接口防刷

    最近开发了一个功能,需要发送短信验证码鉴权,考虑到短信服务需要收费,因此对此接口做了防刷处理,实现方式主要是Redis+自定义注解(需要导入Redis的相关依赖,完成Redis的相关配置,gs代码,这 ...

  3. git 修改东西之后提交命令

    1.git add * 添加东西 2.git status 查看要提交的东西 3.git commit -m "已经修改LogController文件" 提交 4.pwd 看当前目 ...

  4. 使用.NET 6开发TodoList应用(5.1)——实现Repository模式

    需求 经常写CRUD程序的小伙伴们可能都经历过定义很多Repository接口,分别做对应的实现,依赖注入并使用的场景.有的时候会发现,很多分散的XXXXRepository的逻辑都是基本一致的,于是 ...

  5. mysql报错:You do not have the SUPER privilege and binary logging is enabled

    MySQL出现 You do not have the SUPER privilege and binary logging is enabled报错 解决方案: 1.用root用户登录:mysql ...

  6. OnZoom 基于Apache Hudi的流批一体架构实践

    1. 背景 OnZoom是Zoom新产品,是基于Zoom Meeting的一个独一无二的在线活动平台和市场.作为Zoom统一通信平台的延伸,OnZoom是一个综合性解决方案,为付费的Zoom用户提供创 ...

  7. 【LeetCode】565. Array Nesting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)

    当时比赛时超时了,那时没学过树状数组,也不知道啥叫离散化(貌似好像现在也不懂).百度百科--离散化,把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 这道题是dp题,离散化和树状数 ...

  9. 第二十三个知识点:写一个实现蒙哥马利算法的C程序

    第二十三个知识点:写一个实现蒙哥马利算法的C程序 这次博客我将通过对蒙哥马利算法的一个实际的实现,来补充我们上周蒙哥马利算法的理论方面.这个用C语言实现的蒙哥马利算法,是为一个位数为64的计算机编写的 ...

  10. Chapter 11 Why Model ?

    目录 11.1 Data cannot speak for themselves 11.2 Parametric estimators of the conditional mean 11.3 Non ...