题目

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.

For example,

Consider the following matrix:

[

[1, 3, 5, 7],

[10, 11, 16, 20],

[23, 30, 34, 50]

]

Given target = 3, return true.

分析

二分搜索算法在二维矩阵中的应用;

两次二分搜索:

第一次,二分搜索第一列(借助另一个数组)找出小于等于target的下标pos

第二次,二分搜索pos行,搜索target

AC代码


class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty())
return false; int m = matrix.size(); //将矩阵首列,压入临时vector,寻找,目标元素所在的行
vector<int> v;
for (int i = 0; i < m; i++)
v.push_back(matrix[i][0]); //寻找 <=target 最近的元素下标
int pos = binarySearchPos(v, 0, m - 1, target); //不存在任何一行中
if (pos == -1)
return false;
else if(matrix[pos][0] == target)
return true;
else
return binarySearch(matrix[pos], 0, matrix[pos].size() - 1, target); } int binarySearchPos(vector<int> &nums, int lhs, int rhs, int &target)
{
if (nums.empty())
return -1; while (lhs <= rhs)
{
int mid = (lhs + rhs) / 2; if (nums[mid] == target)
return mid; else if (nums[mid] < target)
{
lhs = mid + 1;
}
else{
rhs = mid - 1;
}//else
}//while if (lhs < rhs && nums[lhs] < target)
{
return lhs;
}
else{
return lhs - 1;
}
} bool binarySearch(vector<int> &nums, int lhs, int rhs, int &target)
{
if (nums.empty())
return false; while (lhs <= rhs)
{
int mid = (lhs + rhs) / 2; if (nums[mid] == target)
return true; else if (nums[mid] < target)
{
lhs = mid + 1;
}
else{
rhs = mid - 1;
}//else
}//while
return false;
}
};

GitHub测试程序源码

LeetCode(74) Search a 2D Matrix的更多相关文章

  1. (LeetCode74)Search a 2D Matrix

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

  2. LeetCode(74):搜索二维矩阵

    Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  3. LeetCode(81) Search in Rotated Array II

    题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...

  4. LeetCode(33)Search in Rotated Sorted Array

    题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...

  5. LeetCode(35) Search Insert Position

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

  6. LeetCode(34)Search for a Range

    题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...

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

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

  8. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  9. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

随机推荐

  1. 51Nod 1649 齐头并进

    #include <iostream> #include <algorithm> #include <cstring> //两遍迪杰斯特拉 #define INF ...

  2. hdu1102 Constructing Roads 基础最小生成树

    //克鲁斯卡尔(最小生成树) #include<cstdio> #include<iostream> #include<algorithm> using names ...

  3. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  4. Planning CodeForces - 854C

    Planning CodeForces - 854C 题意:有n架航班,第i架原先的时候是在第i分钟起飞的.现在前k分钟无法有飞机起飞,因此需要调整安排表,延后飞机起飞.仍然要求每一分钟只有一架飞机起 ...

  5. 根据 目录号 案卷号 用户名 查询 page 中 的条数

    select count(*) from am_b_page a join am_b_entry b on a.entry_id=b.entry_id where b.catalogue_code=' ...

  6. HBase备份恢复练习

    一.冷备 1.创建测试表并插入测试数据 [root@weekend05 ~]# hbase shell hbase(main):005:0> create 'scores','grade','c ...

  7. jquery 选择器包含特殊字符

    选择器包含 : .# ( ] 等等 比如 <div id="id#a"></div> <div id="id[1]">< ...

  8. Properties没有被注意的地方

    源起: 今天阅读源码时发现一个地方不理解: 为什么以下代码第10行 get() 之后value为null时还去 getProperty() 呢? org.springframework.util.Co ...

  9. AJPFX关于Timer类的学习

    * Timer类:计时器public class Demo1 { public static void main(String[] args) throws InterruptedException ...

  10. 【转】一个Java对象到底占多大内存?

    最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...