1. 题目

2. 解答

2.1. 方法一

从矩阵的左下角开始比较

  • 目标值等于当前元素,返回 true;
  • 目标值大于当前元素,j 增 1,向右查找,排除掉此列上边的数据(都比当前元素更小);
  • 目标值小于当前元素,i 减 1,向上查找,排除掉此行右边的数据(都比当前元素更大)。
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) { int i = matrix.size() - 1;
int j = 0;
while (i >=0 && j < matrix[0].size())
{
if (target == matrix[i][j]) return true;
else if (target > matrix[i][j]) j++;
else i--;
}
return false;
}
};
2.2. 方法二

\[\begin{array} {ccc|cc}1&4&7&11&15 \\ 2&5&8&12&19 \\ 3&6&\boxed9&16&22 \\ \hline 10&13&14&\boxed{17}&24 \\ 18&21&23&26&30 \end{array}
\]

我们先沿着对角线的方向,找到第一个大于目标值的数字。比如目标值 14,我们发现 9<14<17。然后左上角和右下角的元素都可以排除掉了。我们只需再对左下角剩余的行和右上角剩余的列分别进行二分查找即可。

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();
if (n == 0) return false; if (m == 1) return binary_row_search(matrix, 0, n-1, target);
if (n == 1) return binary_col_search(matrix, 0, m-1, target); int square = m <= n ? m : n;
int i = 0;
for (i = 0; i < square - 1; i++)
{
if (target == matrix[i][i] || target == matrix[i+1][i+1]) return true;
else if (target > matrix[i][i] && target < matrix[i+1][i+1]) break;
} for (int row = i+1; row < m; row++)
{
if (binary_row_search(matrix, row, i, target)) return true;
} for (int col = i+1; col < n; col++)
{
if (binary_col_search(matrix, col, i, target)) return true;
} return false;
} // 行搜索
bool binary_row_search(vector<vector<int>>& matrix, int row, int end, int target)
{
int start = 0;
while (start <= end)
{
int mid = start + ((end - start) >> 2); // 右移运算优先级小于加法,切记加括号!!!
if (matrix[row][mid] == target) return true;
else if (matrix[row][mid] < target) start = mid + 1;
else end = mid - 1;
}
return false;
} // 列搜索
bool binary_col_search(vector<vector<int>>& matrix, int col, int end, int target)
{
int start = 0;
while (start <= end)
{
int mid = start + ((end - start) >> 2);
if (matrix[mid][col] == target) return true;
else if (matrix[mid][col] < target) start = mid + 1;
else end = mid - 1;
}
return false;
}
};

获取更多精彩,请关注「seniusen」!

LeetCode 240——搜索二维矩阵 II的更多相关文章

  1. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  2. Java实现 LeetCode 240 搜索二维矩阵 II(二)

    240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...

  3. Leetcode 240.搜索二维矩阵II

    搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...

  4. LeetCode 240 - 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列.每列的元素从上到下升序排列.示例: 现有矩阵 matrix 如 ...

  5. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II)

    题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 m ...

  6. Java实现 LeetCode 240 搜索二维矩阵 II

    public static boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0) return false ...

  7. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  8. 【LeetCode】 240. 搜索二维矩阵 II

    题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 mat ...

  9. 240. 搜索二维矩阵 II

    二维数组搜索 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...

随机推荐

  1. 搭建 github.io 博客站点

    前言 很多人都有搭建博客或知识库站点的想法,可自己买云服务器太不划算,部署管理也是个问题:基于免费又热门的 GitHub Pages 来搭建博客站点倒是省钱省力省事的好办法,于是上网一搜,满屏都是关于 ...

  2. grid 布局的使用

    grid 布局的使用 css 网格布局,是一种二维布局系统. 浏览器支持情况:老旧浏览器不支持, 概念: 网格容器.元素应用dispalay:grid,它是所有网格项的父元素. <div cla ...

  3. 纯 js 实现跨域接口调用 jsonp

    开发「bufpay.com 个人即时到账收款平台」的时候,支付页面需要 poll轮询 查询订单状态. bufpay 支付接口如下: 接口地址:https://bufpay.com/api/pay/ai ...

  4. 个人免签收款接口 bufpay.com 支持限额设置

    有产品希望收款分布到不同的手机,每个当手机达到某一限额以后就停止改手机的收款. bufpay.com 近期上线了收款限额设置功能,配置界面如下图: 每个手机微信或支付宝可以单独设置每日限额,如果该手机 ...

  5. 竞赛题解 - [CF 1080D]Olya and magical square

    Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...

  6. hdu_4465_Candy

    LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large cand ...

  7. Windows远程常见问题

    1.window2003远程桌面“已达最大连接数”解决:1)mstsc /v:(此处为服务器IP) /console   任务管理器注销已断开用户 mstsc /v:192.168.4.3 /cons ...

  8. Flask第三方组件之flask_session

    flask默认提供了session, 但是存在以下问题: ① session数据存在客户端, 不安全 ② 大小有限制 ③ 增加了客户端的压力 所以才产生了很多第三方的session机制, 我使用的是f ...

  9. xp sp3安装.Net 4.0提示严重错误,0x80070643,解决办法2017版

    客户电脑上要装金税开票软件,需要.net 4.0.30319.1,电脑环境是xp sp3,已经安装了.net 2, .net 3.5sp1,安装.net 4.0的时候提示错误0x80070643 因为 ...

  10. Hbase过滤器

    Hbase过滤器简介 HBase的基本API,包括增.删.改.查等,增.删都是相对简单的操作,与传统的RDBMS相比,这里的查询操作略显苍白,只能根据特性的行键进行查询(Get)或者根据行键的范围来查 ...