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. [SCOI2007]组队 差分

    题面:[SCOI2007]组队 题解: 一开始固定H然后找性质找了很久也没有找到任何有用的东西...... 然后大佬告诉我一个神奇的方法... 首先我们化一波式子: 设$H$表示高度的最小值,$V$表 ...

  2. ARC072 D Alice&Brown 博弈论

    ---题面--- 题解: 题目大意:有2堆石子数分别为x, y的石子,你每次可以从中间的某一堆中取出2i个石子,扔掉i个,并把剩下的i个放到另一堆,无法操作的人就输了. 现在给定x,y,判断先手必赢还 ...

  3. Android <Android应用开发实战> 学习总结杂项

    1.系统相册默认保存地址:android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Ca ...

  4. BZOJ1857:[SCOI2010]传送带——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1857 Description 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送 ...

  5. BZOJ1042 [HAOI2008]硬币购物 【完全背包 + 容斥】

    1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2924  Solved: 1802 [Submit][St ...

  6. 洛谷 P4597 序列sequence 解题报告

    P4597 序列sequence 题目背景 原题\(\tt{cf13c}\)数据加强版 题目描述 给定一个序列,每次操作可以把某个数\(+1\)或\(-1\).要求把序列变成非降数列.而且要求修改后的 ...

  7. 51nod 1486 大大走格子(DP+组合数学)

    枚举不合法点的思想. 把障碍x坐标为第一关键字,y坐标为第二关键字排序.f[i]表示走到第i个障碍的方案数. f[i]=C(x[i]+y[i]-2,x[i]-1)-sigma(f[j]*C(x[i]- ...

  8. PHP实现---汉字简体繁体转换

    這是以class包裝好的簡繁體轉換的class, 另外需要兩個table檔: 1. http://www.jerry.com.tw/php/big5.map 2. http://www.jerry.c ...

  9. httpClient需要的jar包

  10. 学习opencv-------函数使用一

    #include"head.h" //cvResize() /*int main() { IplImage *img = cvLoadImage("e:/picture/ ...