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. linux下记录入站请求

    将内网机器通过frp映射到公网后,内网主机受到大量ssh爆破攻击,攻击来源为frp的服务端,仅在内网机器上无法追踪到真实的攻击来源的ip.下面记录了在frp服务端监控指定端口的入站数据,找到真正的攻击 ...

  2. JAVA中Base64和byte数组(byte[]) 相互转换

    Base64转byte[] byte[] bytes = DatatypeConverter.parseBase64Binary("base64字符串"); byte[]转base ...

  3. JAVA获取昨天、今天、明天等日期

    /** * 获取明天的日期字符串 * @return */ public static String tomorrowDateStr(){ Date date=new Date();//取时间 Cal ...

  4. Once Again...

    Once Again... 题目链接 题意 给n个数,然后T次循环后组成一个新的数列,求这个数列的最长不递减子序列. 思路 因为最多就100个元素,所以当m<=100的时候直接暴力求最长不递减子 ...

  5. Mysterious For(hdu4373)

    Mysterious For Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. Adversarial Examples Improve Image Recognition

    Xie C, Tan M, Gong B, et al. Adversarial Examples Improve Image Recognition.[J]. arXiv: Computer Vis ...

  7. kotlin+springboot+mybatis-puls+mysql搭建gradle-web工程

    kotlin+springboot+mybatis-puls+mysql搭建web工程 ​ 前段时间研究了spring security及OAuth2系列之后,本来打算研究spring的,但是部门发生 ...

  8. 昆泰CH7511B方案|EDP转LVDS资料|CS5211pin to pin 替代CH7511B电路设计

    Chrontel的CH7511B是一种低成本.低功耗的半导体器件,它将嵌入式DisplayPort信号转换为LVDS(低压差分信号).这款创新的DisplayPort接收机带有集成LVDS发射机,专为 ...

  9. 【jvm】08-垃圾回收器那么多傻傻分不清?

    [jvm]08-垃圾回收器那么多傻傻分不清? 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮助到你的话请顺手点 ...

  10. 万能密码:‘or 1=1-- 实战SQL注入,秒破后台

    主要是没有对登录密码的字符串进行参数化和过滤,所以导致网站可以直接用"万能密码"进行突破登录 仅供学习交流 这是某同学做的网站,今天无聊打开了,并帮他进行测试一下 看到这个后台,感 ...