LeetCode 240——搜索二维矩阵 II
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. 方法二
\]
我们先沿着对角线的方向,找到第一个大于目标值的数字。比如目标值 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的更多相关文章
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- Java实现 LeetCode 240 搜索二维矩阵 II(二)
240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...
- Leetcode 240.搜索二维矩阵II
搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...
- LeetCode 240 - 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列.每列的元素从上到下升序排列.示例: 现有矩阵 matrix 如 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II)
题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 m ...
- Java实现 LeetCode 240 搜索二维矩阵 II
public static boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0) return false ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- 【LeetCode】 240. 搜索二维矩阵 II
题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 mat ...
- 240. 搜索二维矩阵 II
二维数组搜索 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...
随机推荐
- Vue--- 一点车项目 连接数据库
Vue--- 一点车项目 连接数据库 创建连接数据库配置 ###导入 const Koa = require('koa'); const Router = require('koa-router') ...
- ie 兼容
ese,promise解决text/babel <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7 ...
- laravel 5.7 resources 本地化 简体中文
使用方法: 新建目录[项目目录/resources/lang/zh] 按以下内容创建文件,并将内容复制到文件中 修改 config/app.php 'locale' => 'zh', 'fall ...
- angular.js-1初识
初识AngularJS AngularJS 为了克服HTML在构建页面上的不足,通过新的属性和表达式扩展了 HTML(AngularJS 通过指令扩展了 HTML,且通过表达式绑定数据到 HTML). ...
- ajaxSubmit 在ie9或360兼容中,form下是空的
解决办法:在<head>....</head>中加入<meta http-equiv="X-UA-Compatible" content=" ...
- 对象转换成JSON字符串
定义一个Student类: 1 class Student { 2 public $name; 3 public $age; 4 function __construct($name, $age) { ...
- 全文检索引擎 sphinx-coreseek中文索引
Sphinx是一个基于SQL的全文检索引擎,可以结合MySQL,PostgreSQL做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索. Sphinx特别为一些 ...
- java并发(1)
hashmap效率高单线程不安全,hashTable效率低但线程安全 因为hashTable使用synchronized来保证线程安全,所以效率十分低,比如线程1使用put插入数据时,线程2既不能使用 ...
- BugkuWeb本地包含
知识点:$_REQUEST不是一个函数,它是一个超全局变量,里面包括有$_GET $_POST $_COOKIE的值,$_REPUEST 是接收了 $_GET $_POST $_COOKIE 三个的集 ...
- 观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录 ASP.NET Core MVC入门 Asp.Net Core启动和配置 Program类,Main方法 Startup类 依赖注入,I ...