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

class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int c = n-1;
for(int r = 0;(r<m)&&(c>=0);)
{
if(matrix[r][c]>target)
{
c--;
}
else if(matrix[r][c]<target)
{
r++;
}
else
return true;
}
return false;
}
//每次比较行末,由于已经排序好,可以进行行列删除
};

  

search-a-2d-matrix(二维矩阵查找)的更多相关文章

  1. Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。

    问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找. 算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法. public class SearchInSortedM ...

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

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

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

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

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

  7. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

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

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

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

随机推荐

  1. Android 图片圆角的设置

    ImageView的scaleType的属性有好几种,分别是matrix(默认).center.centerCrop.centerInside.fitCenter.fitEnd.fitStart.fi ...

  2. [转]为什么不能用memcached存储Session

    以下内容转自:http://www.infoq.com/cn/news/2015/01/memcached-store-session -------------------------分割线---- ...

  3. Java:Map

    接口Map<K,V>,K – 映射所维护的键的类型:V – 映射值的类型. public interface Map<K,V>,将键映射到值的对象.一个映射不能包括重复的键:每 ...

  4. iOS开发UI篇—UIScrollView控件实现图片轮播

    iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "YYV ...

  5. iOS开发多线程篇—GCD的常见用法

    iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...

  6. windows下上传文件到VWware的centos

    1.使用Xsell 连接到centos虚拟机,可以在虚拟机使用终端查看ip地址,命令:ifconfig -a 2.远程Linux系统上需要安装lrzsz工具包  Xsell命令:yum install ...

  7. 用Handler图片轮播练习

    XML代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andr ...

  8. androidd 程序默认安装位置和数据存储位置(公用和私用)

    默认安装位置: android App 安装到外置SD卡中,缓解手机内置内存的压力: <manifest xmlns:android="http://schemas.android.c ...

  9. php一个简单的计算器

    <?php if(isset($_POST['sub'])) { $result = ''; switch($_POST['ysf']) { case '+': $result = $_POST ...

  10. 2016 - 1- 24 大文件下载 关于NSOutStream 的使用补充

    // // ViewController.m // 大文件下载 // // Created by Mac on 16/1/24. // Copyright © 2016年 Mac. All right ...