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. 11个Visual Studio代码性能分析工具

    软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...

  2. (1)apply族函数总论

                  来自为知笔记(Wiz) 附件列表

  3. iOS 修改UIWebView的UserAgent

    iOS和H5交互的时候,H5需要用userAgent带一些参数,需要我们修改默认的UserAgent为自定义的. 首先,给大家普及一下userAgent的历史,点击UserAgent查看. 1 在Ap ...

  4. C#3.0 特性

    C#3.0特性 隐式类型的本地变量和数组 对象初始值设定项 集合初始值设定项 扩展方法 匿名类型 lambda表达式 查询关键字 自动实现的属性 分布方法定义 lambda表达式与表达式树 https ...

  5. src 小心得

    关于src的引用,不要用相对路径,  ../   虽然省事,但是跳转页面时容易出错. 举个例子: 在web页面引用  D:\phpStudy\WWW\ueditor\utf8-php 这个文件夹下面  ...

  6. jquery选择器(一)-基础选择器

    1. ID元素选择器 $("#btn1") 2. class元素选择器 $(".btn") 3. 标签元素选择器 $("div") 4. 全 ...

  7. input多选计算

    CSS代码: body { counter-reset: icecream; } input:checked { counter-increment: icecream; } .total::afte ...

  8. eclipse中整合springMvc,velocity和sitemesh

    1.项目所需要jar包 (有些可能多余) 2.创建UserController   目录如下: package qust.thb.usermanage.controller; import org.s ...

  9. am335x UART1输入u-boot 调试信息代码修改

    AM335x 调试信息UART1输出代码修改1. 关于pin_mux  的配置代码修改位置:/board/forlinx/ok335x/mux.c void enable_uart0_pin_mux( ...

  10. GCD-Grand Central Dispatch

    经常要用的,总结分类一下. //获得队列 DISPATCH_QUEUE_PRIORTY 优先级 dispatch_queue_t Queue = dispatch_get_global_queue(D ...