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.

实际上就是二分搜索, 不难, 一次AC。

注意一下退出条件是 l <= r 就行。有等于。

class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if(matrix.empty())
return false; int length = matrix.size() * matrix[].size();
int l = , r = length - ; while(l <= r) //注意 这里包含等于
{
int mid = (l + r) / ;
int col = mid % matrix[].size();
int row = mid / matrix[].size();
if(matrix[row][col] < target)
{
l = mid + ;
}
else if(matrix[row][col] > target)
{
r = mid - ;
}
else
{
return true;
} }
return false;
}
};

【leetcode】 Search a 2D Matrix (easy)的更多相关文章

  1. 【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 ...

  2. 【Leetcode】【Medium】Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. 【数组】Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  4. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  6. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

  7. [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. 【题解】【数组】【查找】【Leetcode】Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

随机推荐

  1. [设计模式] javascript 之 组合模式

    组合模式说明 组合模式用于简单化,一致化对单组件和复合组件的使用:其实它就是一棵树: 这棵树有且只有一个根,访问入口,如果它不是一棵空树,那么由一个或几个树枝节点以及子叶节点组成,每个树枝节点还包含自 ...

  2. js 日期处理,json处理

    模块化js :requirejshttp://www.requirejs.cn/ 好用的日期控件:http://www.bootcss.com/p/bootstrap-datetimepicker/i ...

  3. CSS 补充

    属性选择器下面的例子为带有 title 属性的所有元素设置样式:[title]{ color:red;} <h1>可以应用样式:</h1><h2 title=" ...

  4. Jetty安装

    下载jetty http://www.eclipse.org/jetty/  看好jdk 版本 安装 解压压缩包到指定目录,且将其目录路径定义为${JETTY_HOME} 进入${JETTY_HOME ...

  5. 宿主系统为Ubuntu 14,CentOS 6.5 安装VirtualBox增强工具失败:Building the OpenGL support module[FAILED]

    安装先前的笔记:CentOS 6.3 中安装VirtualBOX增强工具失败:Building the main Guest Additions module[FAILED],执行了以下命令 #安装 ...

  6. mysql 修改表结构

    alter table 表名 modify column 字段名 varchar(数量); 将varchar(50)改为255 alter table 表名 modify column 字段名 var ...

  7. javascript高级程序设计---Element对象

    Element对象对应网页的HTML标签元素.每一个HTML标签元素,在DOM树上都会转化成一个Element节点对象(以下简称元素节点).元素节点的nodeType属性都是1,但是不同HTML标签生 ...

  8. sql查询比较两表不同数据与相同数据

    以下举例是查询相同数据,否则则相反 方法一: select * from A as x,B as y where x.a1=y.b1 and x.a2=y.b2 and x.a3=y.b3 方法二: ...

  9. Oracle 中循环遍历某张表,并对符合条件的进行Update操作

    BEGIN FOR L_RECORD IN (select RECORD_ID,CURR_PERIOD,PERIOD_START_DATE, (sysdate- PERIOD_START_DATE) ...

  10. jquery 平滑锚

    setTimeout('$("html,body").animate({ scrollTop: $(".title").offset().top }, 1000 ...