Search a 2D Matrix 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/


Description

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.

Example

For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

Solution

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty() || matrix[0].empty()) {
return false;
} int size = matrix.size();
int low = 0, high = size - 1, mid;
while (low < high) {
mid = (high + low) / 2;
if (target == matrix[mid].back())
return true;
else if (target < matrix[mid].back())
high = mid;
else
low = mid + 1;
}
size = matrix[low].size();
vector<int>& arr = matrix[low];
low = 0;
high = size - 1;
while (low < high) {
mid = (high + low) / 2;
if (target == arr[mid])
return true;
else if (target < arr[mid])
high = mid;
else
low = mid + 1;
}
return arr[low] == target;
}
};

解题描述

这道题考察的是二分查找。我选择的算法是先用二分查找确定target在矩阵的哪一行,再在这一行中进行二分查找找出target。不过AC之后想想,其实可以把矩阵直接看成一维数组进行二分查找,只需要多做一步下标转换即可。

[Leetcode Week13]Search a 2D Matrix的更多相关文章

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

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

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

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

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

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

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

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

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

  9. 【leetcode】 Search a 2D Matrix (easy)

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

随机推荐

  1. C#多线程间的同步问题

    使用线程时最头痛的就是共享资源的同步问题,处理不好会得到错误的结果,C#处理共享资源有以下几种: 1.lock锁 需要注意的地方: 1).lock不能锁定空值某一对象可以指向Null,但Null是不需 ...

  2. Matlab 之 im2col 【转】

    函数原型: B=im2col(A,[m n],block_type) 功能:将矩阵A分为m×n的子矩阵,再将每个子矩阵作为B的一列 (1)当block_type为distinct时,将A分解为互不重叠 ...

  3. filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行

    filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行

  4. BZOJ4517 & 洛谷4071:[SDOI2016]排列计数——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4517 https://www.luogu.org/problemnew/show/P4071 求有 ...

  5. NDK plugin来构建JNI项目(相对于手动构建)

    http://blog.csdn.net/codezjx/article/details/8879670 1.添加ndk环境支持 Android Tools -> Add Native Supp ...

  6. dubbox小demo

    概述: 我们建立两个web项目,一个是service负责提供服务,另一个是web项目负责调用服务. 两个项目都是 maven Project 项目 生产者项目: 项目中主要就是: pom文件,引入相关 ...

  7. 在CentOS 6.5 中安装JDK 1.7 + Eclipse并配置opencv的java开发环境(二)

    一.安装JDK 1.7 1. 卸载OpenJDK rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.50.1.11.5. ...

  8. ELK6.0环境搭建及配置

    ELK环境搭建及配置 ElasticSearch在5.x后的安装和插件的官方执行更好了,head插件官方默认集成在kibana的dev tools里,支持rpm包方式安装,x-pack安装后支持权限及 ...

  9. 正确理解WPF中的TemplatedParent

    (注:Logical Tree中文称为逻辑树,Visual Tree中文称为可视化树或者视觉树,由于名称不是很统一,文中统一用英文名称代表两个概念,况且VisualTreeHelper和Logical ...

  10. Jade模板引擎学习(二)语法:代码、变量、循环、过滤器及mixin

    Jade语法 一.代码 不会被缓冲代码 ul - for(var i=0; i; i++) li Jade Engine 会转换为: <ul> <li>Jade Engine& ...