编写一个高效的算法来搜索 m x n 矩阵中的一个目标值。该矩阵具有以下特性:
    每行中的整数从左到右排序。
    每行的第一个整数大于前一行的最后一个整数。
例如,
以下矩阵:
[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
给定 目标值= 3,返回 true。
详见:https://leetcode.com/problems/search-a-2d-matrix/description/

Java实现:

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int row=matrix.length-1;
int col=0;
while(row>=0&&col<matrix[0].length){
if(matrix[row][col]==target){
return true;
}else if(matrix[row][col]>target){
--row;
}else{
++col;
}
}
return false;
}
}

C++实现:

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

参考:https://blog.csdn.net/zhangxiao93/article/details/49835511

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

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

  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. Leetcode74. Search a 2D Matrix搜索二维矩阵

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

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

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

    Leetcode之二分法专题-240. 搜索二维矩阵 II(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 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

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

随机推荐

  1. smack api 转载未测试

    ===============================================================主动发送信息给某个用户-------------------------- ...

  2. 勤于思考:Objective-C特性的扩展

    赋值 assign:直接赋值.默认 @interface Car : NSObject { NSString *_name; } @property (assign,nonatomic) NSStri ...

  3. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  4. dd备份文件系统

    1.实现dd的备份: 使用gzip压缩: dd if=/dev/hdb | gzip > /local/path/image.gz 说明:/dev/hdb 是硬盘整盘.对不同的硬盘,可能是 /d ...

  5. ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)

    Description Let's consider one interesting word game. In this game you should transform one word int ...

  6. 洛谷 P4336 黑暗前的幻想乡 —— 容斥+矩阵树定理

    题目:https://www.luogu.org/problemnew/show/P4336 当作考试题了,然而没想出来,呵呵. 其实不是二分图完美匹配方案数,而是矩阵树定理+容斥... 就是先放上所 ...

  7. win10 设备摄像头,麦克风,【隐私】权限

    win10 因为隐私问题, 把mic,摄像头, 定位功能关闭,  之后调用USB摄像头的时候,忘了这个, 接口API 一直返回调用失败,[不能创建视频捕捉过滤器 hr=0x80070005] => ...

  8. Sybase.在PowerDesigner中如何添加-约束

    Sybase.在PowerDesigner中如何添加-约束 资料 2010-06-23 08:57:09 阅读21 评论0   字号:大中小 订阅 唯一约束 唯一约束与创建唯一索引基本上是一回事,因为 ...

  9. Js获取当前的日期和时间以及时间戳转化为时间

    /** *获取当前时间 *format=1精确到天 *format=2精确到分 */ function getCurrentDate(format) { var now = new Date(); v ...

  10. 散列表(Hash table)及其构造

    散列表(Hash table) 散列表,是根据关键码值(Key value)而直接进行访问的数据结构.它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映射函数叫做散列函数,存放记录 ...