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. IIS部署,发布网站

    一.IIS部署 1.打开控制面板,选择 '程序' 2.程序和功能下,选择打开或关闭Windows功能 3.等待加载,选择Internet信息服务,勾选如下选项 在弹出的"windows功能& ...

  2. docker查看容器元数据、详细信息,查看容器挂载的目录

    通过 docker inspect 175f 查看容器元数据 我们启动docker的时候会挂载目录,但是挂载之后 后面就忘了 如何查看挂载的目录位置呢 可以通过 docker inspect a7a6 ...

  3. 【LeetCode】801. Minimum Swaps To Make Sequences Increasing 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 参考资料 日期 题目地址:https:// ...

  4. 1067 - Combinations

    1067 - Combinations   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Giv ...

  5. 【C\C++笔记】数组指针越界

    指针越界,t的数组指针越界,修改了c的内容. 使用指针时,必须规定指针移动的范围 #include <iostream> using namespace std; int main(){ ...

  6. 第二十九个知识点:什么是UF-CMA数字签名的定义?

    第二十九个知识点:什么是UF-CMA数字签名的定义? 第16篇博客给出了DSA,Schnoor和RSA-FDH签名方案的细节,但是签名方案是什么?它应该保证什么样的安全性? 一个签名方案\(S\)是一 ...

  7. CS5263替代PS176|设计DP1.4转HDMI2.0音视频转换线方案|PS176方案

    PS176是一个显示端口 (DP)至HDMI 2.0视频接口转换器适用于需要视频协议转换的电缆适配器.电视接收器.监视器和其他应用.它将接受任何显示端口输入格式,包括DP 1.1a.dp1.2a.dp ...

  8. SpringMVC+Spring+Mybatis实现登录注册Demo

    使用环境:MyEclipse/Eclipse + Tomcat + MySql. 使用技术:SpringMVC + Spring + Mybatis. 实现效果 登录页面: 密码错误提示 登录成功后 ...

  9. 大二 mysql高级+html响应式+Java高级50道试题

    1.CSS3中过渡属性 transition-timing-function的值包括哪些 A. ease B. inline C. ease-in D. easeout 答案:A,C 解析:过渡属性 ...

  10. 云南农职《JavaScript交互式网页设计》 综合机试试卷⑥——简易旅游网

    本页面分为顶部导航.登录注册栏,中部图片展示.主体内容和底部反馈模板 一.导航栏部分 要求一:设置菜单栏(二级菜单)和登录注册模块 要求二:当鼠标悬停到菜单栏(一级菜单)时,二级菜单以滑动效果滑出显示 ...