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. Oracle在线重定义(online redefinition)--将普通表改为分区表

    使用Oracle的在线重定义技术,可以将Oracle的普通表改为分区表.操作如下: STEP1:测试表是否可以在线重定义,这里以unixdev数据库的LIJIAMAN.BSTEST为例 EXEC DB ...

  2. 团体队列 UVA540 Team Queue

    题目描述 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么新人会插队到最后一个队友的身后.如果没有任何一个队友排队,则他会被排到长队的队尾. 输入每个团队中所有队员的编号,要求 ...

  3. ABAP术语-RFC (Remote Function Call)

    RFC (Remote Function Call) 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/12/1101581.html RFC ...

  4. memcache和redis的区别和联系

    一.区别 Memcache : 1,对每个key的数据最大是1M. 2,对各种技术支持比较全面,session可以存储memcache中,各种框架(例如thinkphp)对memcache支持比较好. ...

  5. 利用haohedi ETL将数据库中的数据抽取到hadoop Hive中

    采用HIVE自带的apache 的JDBC驱动导入数据基本上只能采用Load data命令将文本文件导入,采用INSERT ... VALUES的方式插入速度极其慢,插入一条需要几十秒钟,基本上不可用 ...

  6. 中国大学MOOC-C程序设计(浙大翁恺)—— 时间换算

    时间换算(10分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果小 ...

  7. 小程序开发-10-新版Music组件、组件通信与wxss样式复用

    加入缓存提升用户体验 思路:先从缓存中寻找数据或者从服务器中获取数据写入缓存中 优点:减少网络访问次数,提升用户体验 解决缓存带来的问题 问题:比如原先是不喜欢的在点击喜欢的时候,跳到下一期刊后返回来 ...

  8. Nodejs实战 —— 测试 Node 程序

    读 <node.js实战2.0>,进行学习记录总结. 当当网购买链接 豆瓣网1.0链接 测试 Node 程序 本章内容 用 Node 的 assert 模块测试 使用其他断言库 使用 No ...

  9. react前置路由守卫

    react中一切皆组件-- 目标:自定义user界面的前置路由守卫,当用户点击要进入user组件时,路由守卫发起判断,如果条件满足则进入,否则跳转至login组件. .入口文件index.js中代码如 ...

  10. 20145202马超《java》实验5

    两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA 结对实现中缀表达式转后缀表达式的功能 MyBC.java 结对实现从上面 ...