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.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ...
随机推荐
- ubuntu下安装memcached和PHP的memcache扩展
依赖包和软件包下载地址: Libevent:https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/li ...
- centos6,python3,通过pip安装pycurl出现报错提示
Centos6.7系统,python3.6.7,通过 pip 安装pycurl出现报错: __main__.ConfigurationError: Could not run curl-config: ...
- 【memcached启动报错】
#前台启动不了 #指定-u root #后台启动 #扩展选项: #利用telnet连接memcached 的端口登录memcached服务 #error表示有语法错误 #store表示正确
- 第一篇:百问网ubuntu安装注意事项和部分配置
目录 一.开启虚拟化技术 二.ubuntu部分设置 一.开启虚拟化技术 64位机,需要使用cpu-z.SecurAble软件来检查:CPU是否支持VT虚拟化技术 cpu-z使用(软件) 第一步:以 ...
- 网络文件系统nfs在ubuntu16.04的安装
1.搜索nfs-sudo apt-cache search nfs- 2.安装sudo apt-get install nfs-kernel-server 3.配置:/etc/exports /hom ...
- %.*lf控制输出长度
#include<stdio.h> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&a ...
- C语言调整数组使奇数全部都位于偶数前面
//输入一个整数数组,实现一个函数,//来调整该数组中数字的顺序使得数组中所有的奇数 位于数组的前半部分,//所有偶数 位于数组的后半部分. #include<stdio.h>#inclu ...
- 函数调用方法之__cdecl与_stdcall
在debug VS c工程文件时,碰到cannot convert from 'int (__cdecl *)(char *)' to 'xxx'这个问题,发现问题在于typedef函数指针类型时,将 ...
- linux下Intellij Idea 14的安装
一.安装配置jdk 虽然很多Linux发行版现在已经自带OpenJDK,但是在开发过程中 ...
- Redis系列六 Redis事务
Redis事务 1.介绍 在Redis事务中可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺序地串行化执行而不会被其它命令插入,不许加塞. 2.事务的作用 一个队列中, ...