题意:

  有一个矩阵,每行都有序,每行接在上一行尾后仍然有序。在此矩阵中查找是否存在某个数target。

思路:

  这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了。由于用vector装的,但是也是满足线性的。

  二分:O(log n*m)

 class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
int n=matrix.size(), m=matrix[].size();
int L=, R=n*m-;
while(L<R)
{
int mid=L+(R-L+)/;
if(matrix[mid/m][mid%m]<=target) L=mid;
else R=mid-;
}
return matrix[L/m][L%m]==target;
}
};

AC代码

  

  迭代:O(n+m)

 class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
int n=matrix.size(), m=matrix[].size();
int i=, j=m-;
while(i<n && j>=)
{
if(target==matrix[i][j]) return true;
if(target>matrix[i][j]) i++;
else j--;
}
return false;
}
};

AC代码

LeetCode Search a 2D Matrix(二分查找)的更多相关文章

  1. 74. Search a 2D Matrix(二分查找,剑指offer 1)

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

  2. [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 ...

  3. [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 ...

  4. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  5. 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 ...

  6. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

  7. [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找

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

  8. leetcode——Search a 2D Matrix 二维有序数组查找(AC)

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

  9. 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 ...

  10. [leetcode]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

随机推荐

  1. splunk 索引过程

    术语: Event :Events are records of activity in log files, stored in Splunk indexes. 简单说,处理的日志或话单中中一行记录 ...

  2. ANGULAR JS WATCH监听使用

    ANGULAR 监听使用: 当angular数据模型发生变化时,我们需要如果需要根据他的变化触发其他的事件. $watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你. ...

  3. shell脚本初析

    简单理解:运用很多工具,将复杂的步骤简单化,体现在shell脚本中框架:必须有备注,写的别人能够看得懂开头:#! /bin/bash 代表的意思是改文件使用的是bash语法要想使用该种方法运行shel ...

  4. tortoisegit教程

    tortoisegit教程: http://www.mamicode.com/info-detail-311565.html https://my.oschina.net/longxuu/blog/1 ...

  5. Genymotion常见问题整合与解决方案

    常见问题1:Genymotion在开启模拟器时卡在了starting virtual device(注意只有tarting virtual device窗口,没有模拟器的黑屏窗口)    原因:Vir ...

  6. if语句使用

    package yuan; public class Yuan { public static void main(String[] args) { int a = 1; int b = 8; int ...

  7. routeProvider

    In a previous post about testing I mentioned that route resolves can make authoring unit tests for a ...

  8. 面试题目-findmax的实现

    #include <vector> #include <iostream> #include "printCollection.h" using names ...

  9. Android SDK生成时,自定义文件名称,而非系统第一分配的app-release.apk

    buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.tx ...

  10. Spring学习笔记之初始化和销毁方法的调用次序

    Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, a ...