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. ffmpeg 简单使用总结

    FFMPEG 生成指定长度的空白音频: ffmpeg -f lavfi -i aevalsrc=0 -t seconds -q:a 9 -acodec libmp3lame out.mp3 FFMPE ...

  2. Unity 游戏框架搭建 (二十一) 使用对象池时的一些细节

    上篇文章使用SafeObjectPool实现了一个简单的Msg类.代码如下: class Msg : IPoolAble,IPoolType { #region IPoolAble 实现 public ...

  3. Inconsistant light map between PC and Mobile under Unity3D

    Author: http://www.cnblogs.com/open-coder/p/3898159.html The light mapping effects between PC and Mo ...

  4. 冒泡排序_c++

    冒泡排序_c++ GitHub 文解 冒泡排序是采用类似气泡上升的方式对数据进行排序. 例如: 我们这里有10个元素,具体数值随意,对每个数值标记上 1~10 的标记. 首先将标记为 1 的数值与标记 ...

  5. ABAP术语-Transaction

    Transaction 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/19/1112804.html Logical process in ...

  6. linux下pip错误 ImportError: No module named 'pip_internal'

    wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate sudo python get-pip.py

  7. Python豆瓣源

    pip install -i https://pypi.doubanio.com/simple/ xxxx

  8. react路由配置(未完)

    React路由 React推了两个版本 一个是react-router 一个是react-router-dom 个人建议使用第二个 因为他多了一个Link组件 Npm install react-ro ...

  9. Redis总导航目录

    NoSQL入门和概述 NoSQL入门概述 3V + 3高 当下的NoSQL经典应用 NoSQL数据模型简介 NoSQL数据库的四大分类 在分布式数据库中CAP原理CAP+BASE Redis入门介绍 ...

  10. Redis(九):Redis的Java客户端Jedis

    Redis的Java客户端Jedis导航目录: 安装JDK 安装Eclipse Jedis所需要的Jar包 Jedis常用操作 JedisPool 安装JDK tar -zxvf jdk-7u67-l ...