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: [
[, , , ],
[, , , ],
[, , , ]
]
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
int n = matrix[].size();
if(target < matrix[][] || target >matrix[m-][n-])return false; int i,j; for(i = ;i< m-;i++)
if(target >=matrix[i+][])
continue;
else
break ; for(j = ; j< n; j++)
if(target == matrix[i][j])
return true;
else if(target <matrix[i][j])
return false;
}
};

感觉上面的方法虽然过了所有case,但还是可能有问题的,看了《剑指offer》后,才发现了一种更好的解法:

class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
int n = matrix[].size(); int row = , column = n -;
while(row < m && column >= )
{ if(matrix[row][column] == target)
return true;
else if(matrix[row][column] > target)
column--;
else
row++;
}
return false;
}
};

LeetCode_Search a 2D Matrix的更多相关文章

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

  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. HTML 学习笔记 CSS3 (2D Matrix)

    Matrix 矩阵 那么什么是矩阵呢? 矩阵可以理解为方阵,只不过 平时方阵里面站着人 矩阵中是数值: CSS3中的矩阵: css3中的矩阵指的是一个方法,书写为matrix() 和 matrix3d ...

  4. Leetcode 74 and 240. Search a 2D matrix I and II

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

  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. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  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. Search a 2D Matrix | & II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...

  9. LeetCode Search a 2D Matrix II

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

随机推荐

  1. Qt的Script、Quick、QML的关系与总结

    背景 最近在学QML,感觉也不难,就是一直以来接触 Qt 的脚本类的东西的顺序是Script.Quick1.Declarative.Quick2.QML.那么每一个都是干什么的呢,这些东西搞的我有点混 ...

  2. 程序错误[C/C++]

    对于初学者而言,一般意义上,程序错误可以分为两类,逻辑错误和非逻辑错误.前者是指,程序可以通过编译或链接但运行时不符合预期结果,后者是程序不能通过编译或链接. 乍一看这样的分类非常清楚.不过,当引入语 ...

  3. zabbix 添加自定义key

    vim /etc/zabbix/zabbix_agentd.conf UserParameter=zjzc.login,/bin/sh /usr/sbin/get_login.sh UserParam ...

  4. VS2012 中使用Emacs布局

    微软的反开源行为导致它不断的衰落,问题是还不反省. 下面这篇文章介绍了如何安装emacs布局的插件: http://marxistprogrammer.blog.163.com/blog/static ...

  5. Android中各种onTouch事件

    Android里有两个类 android.view.GestureDetector android.view.GestureDetector.SimpleOnGestureListener 1) 新建 ...

  6. springmvc+mongodb+maven 项目搭建配置

    操作步骤我就不再细化了 项目能运行,测试过了,先上配置,另一篇文章上代码,点击下载源码 项目结构 pom.xml <project xmlns="http://maven.apache ...

  7. PHP设计模式笔记九:装饰器模式 -- Rango韩老师 http://www.imooc.com/learn/236

    装饰器模式(Decorator) 概述 1.装饰器模式可以动态地添加修改类的功能 2.一个类提供了一项功能,如果要在修改并添加额外的功能,传统的编程模式,需要写一个子类继承它,并重新实现类的方法 3. ...

  8. input文本框获取焦点和失去焦点判断

    onBlur:当输入框失去焦点后 onFocus:当输入框获得焦点后 这两个JavaScript事件是写在html标签中的例如: <input type="text" onB ...

  9. WebSphere之wasprofile.sh使用

    概要文件(profile) 6.0版本以后才有profile,目的是将用户数据和was本身的文件分开,这样可以定义多个profile,每个profile相当于一个用户,相当于提供了多用户的支持. pr ...

  10. SqlBulkCopy使用心得 (大量数据导入)

    文章转载原地址:http://www.cnblogs.com/mobydick/archive/2011/08/28/2155983.html 最近做的项目由于之前的设计人员懒省事,不按照范式来,将一 ...