LeetCode74 Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true. (Medium)
分析:
考察题目给的二维数组的特点发现,其本质跟一个排好序的一维数组没有区别,所以就是一个二分查找问题。
对于mid,其对应的点是matrix[mid / n][mid % n]
代码:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size(), n = matrix[].size();
int start = , end = m * n - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (matrix[mid / n][mid % n] == target) {
return true;
}
else if (matrix[mid / n][mid % n] < target) {
start = mid;
}
else {
end = mid;
}
}
if (matrix[start / n][start % n] == target) {
return true;
}
if (matrix[end / n][end % n] == target) {
return true;
}
return false;
}
};
LeetCode74 Search a 2D Matrix的更多相关文章
- Leetcode74. Search a 2D Matrix搜索二维矩阵
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- Search a 2D Matrix | & II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
随机推荐
- 前端插件--fastclick解决点透问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- windows 标准错误重定向
最近在windows上运行tensorflow的时候,出现很多stderr 的信息,干扰了正常的输出:所以我们需要使用操作把这些输出屏蔽: 参考链接:https://support.microsoft ...
- GIT生成公钥和私钥
转载至:https://blog.csdn.net/gwz1196281550/article/details/80268200 打开 git bash! git config --global us ...
- python实例 条件和循环语句
#! /usr/bin/python #条件和循环语句 x=int(input("Please enter an integer:")) if x<0: x=0 ...
- phpBOM头(字符)出现的原因以及解决方法_PHP程序员博客|高蒙个人博客
今天在项目中发现,客户端在使用ajax得到返回值时,无法匹配字符串.总是报错,打开页面接口发现,页面的头部出现了的字符(BOM头),找到问题了,那么直接用代码清除掉即可. php隐形字符 // 如 ...
- scala的插值器
Scala 为我们提供了三种字符串插值的方式,分别是 s, f 和 raw.它们都是定义在 StringContext 中的方法. s 字符串插值器 val a = 2println(s"小 ...
- arcgis地图窗口操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 一些windows server的操作
windows server2008R2的一些操作和arcgis9.3 服务和arcgis9.3安装 1.在虚拟机中安装: https://jingyan.baidu.com/article/0eb4 ...
- Vue.之.安装
Vue.之.安装 第一步npm安装 首先:先从nodejs.org中下载nodejs 直到Finish完成安装. 打开控制命令行程序(CMD),检查是否正常 使用淘宝NPM 镜像 国内直接使用np ...
- POJ3697
/* Memory Time 7096K 2641MS */ #include <iostream> #include <string> using namespace std ...