编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:

  • 每行的元素从左到右升序排列。
  • 每列的元素从上到下升序排列。

示例:

现有矩阵 matrix 如下:

[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ]

给定 target = 5,返回 true。

给定 target = 20,返回 false。

如果从左上角开始,则往右往下都是递增,不能判定到底往哪个方向。

但是如果是右上角或者左下角,则两个方向情况都不同,就能明确的判定是哪种情况了

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

Leetcode240. Search a 2D Matrix II搜索二维矩阵2的更多相关文章

  1. 240 Search a 2D Matrix II 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7 ...

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

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

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

  5. LeetCode240: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 ...

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

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

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

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

  8. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

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

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

随机推荐

  1. day14 python02---字符串

    day02 数字相关的转换 bin() 2进制oct() 8进制hex() 16进制 字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之 ...

  2. VC++ COMBO BOX控件的使用

    1.你在编辑状态下点那个控件的向下的三角形,就出冒出来一个可以调高度的东东.将高度调高,否则在执行时会不能显示下拉选项.   2.为combo box添加选项,在编辑状态下选combo box控件的属 ...

  3. [NOI2007]生成树计数环形版

    NOI2007这道题人类进化更完全之后出现了新的做法 毕姥爷题解: 于是毕姥爷出了一道环形版的这题(test0814),让我们写这个做法 环形的情况下,k=5的时候是162阶递推. 求这个递推可以用B ...

  4. hdu多校第四场 1007 (hdu6620) Just an Old Puzzle 逆序对

    题意: 给你一个数字拼图,问你数字拼图能否能复原成原来的样子. 题解: 数字拼图的性质是,逆序数奇偶相同时,可以互相转化,逆序数奇偶不同,不能互相转化. 因此统计逆序对即可. #include< ...

  5. Wed Nov 01 13:03:16 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended.

    报错:Wed Nov 01 13:03:16 CST 2017 WARN: Establishing SSL connection without server's identity verifica ...

  6. 命令学习_IPCONFIG: DNS cache操作

    IPCONFIG: DNS cache操作 Windows会将解析到的DNS信息缓存,这个机制可以加速重复的域名访问.从DNS Server返回的DNS Response消息中带有"Time ...

  7. 资源-Java:Java资源列表

    ylbtech-资源-Java:Java资源列表 1. 开发软件返回顶部 1.Eclipse https://www.eclipse.org/ 2.IntelliJ IDEA https://www. ...

  8. System.Clollections.IEnumerable.cs

    ylbtech-System.Clollections.IEnumerable.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicK ...

  9. PAT甲级——A1139 First Contact【30】

    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...

  10. PAT甲级——A1112 Stucked Keyboard【20】

    On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...