题意:

  有一个矩阵,每行有序,每列也有序。判断一个数target是否存在于此矩阵中。

思路:

  从右上角开始,如果当前数字<target,则该行作废。若当前数字>target,该列作废。这样下去要么找到,要么到达边界退出。

 class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int n=matrix.size(), m=matrix[].size();
int i=, j=m-;
while(i<n && j>=)
{
if(target==matrix[i][j]) return true;
if(target>matrix[i][j]) i++;
else j--;
}
return false;
}
};

AC代码

LeetCode Search a 2D Matrix II (技巧)的更多相关文章

  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 II

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

  3. LeetCode——Search a 2D Matrix II

    Description: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix ...

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

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

  5. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

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

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

  7. 【LeetCode】240. 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. Thi ...

  8. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

  9. 【刷题-LeetCode】240. 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. Thi ...

随机推荐

  1. UESTC 2016 Summer Training #6 Div.2

    我好菜啊.. UVALive 6434 给出 n 个数,分成m组,每组的价值为最大值减去最小值,每组至少有1个,如果这一组只有一个数的话,价值为0 问 最小的价值是多少 dp[i][j] 表示将 前 ...

  2. Log4J实用配置指南

    转自:http://www.cnblogs.com/licheng/archive/2008/08/23/1274566.html 1         概述 本文档是针对Log4j日志工具的使用指南. ...

  3. Android动画效果生动有趣的通知NiftyNotification(Android Toast替代品)

    NiftyNotification在github上的项目主页是:https://github.com/sd6352051/NiftyNotification NiftyNotification本身又依 ...

  4. POJ 3299 Humidex 难度:0

    题目链接:http://poj.org/problem?id=3299 #include <iostream> #include <iomanip> using namespa ...

  5. PowerMock 遇到的问题——2

    如果我们要测试的类继承另一个类,而且在构造方法中有super():那么怎么Mock那个super语句呢? 在PowerMock中有一个suppressConstructor方法,具体写法如下: Pow ...

  6. 判断comboBox是否选对了绑定的数据库中的项

    实现: comboBox1下拉列表已绑定数据库,将选中的项保存到数据库时,判断是否已选中下拉列表里的项 如果没选中,或者输入了其他的值,和已绑定的数据不匹配,出现提示框 按钮的点击事件中: strin ...

  7. Android Phonebook编写联系人UI加载及联系人保存流程(四)

    2014-01-07 10:23:22 将百度空间里的东西移过来. 5. KindSectionView KindSectionView是何方神圣呢?它又是怎么怎么和一个DataKind,以及一个Ra ...

  8. superobject中 JavaToDelphiDateTime的使用

    procedure TForm1.FormCreate(Sender: TObject); var n: TDateTime; i64: Int64; s: Integer; begin Memo1. ...

  9. seajs 使用 jquery插件

    define(function(require,exports,moudles){ return function(jquery){ (function($) { $.fn.pri= function ...

  10. (转)iOS应用架构谈 view层的组织和调用方案

    前言 <iOS应用架构谈 开篇>出来之后,很多人来催我赶紧出第二篇.这一篇文章出得相当艰难,因为公司里的破事儿特别多,我自己又有点私事儿,以至于能用来写博客的时间不够充分. 现在好啦,第二 ...