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 in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.
[
[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进行比较,如果大于target的话那么这一列都不会存在target,小于的话那么改行都不会存在该元素,从而进行下一步的搜寻,代码如下:

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

LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)的更多相关文章

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

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

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

  4. Leetcode240. Search a 2D Matrix II搜索二维矩阵2

    编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 matrix ...

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

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

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

  8. (medium)LeetCode 240.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 ...

  9. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

  10. Leetcode 240. 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 ...

随机推荐

  1. JVM调优总结(转)

    本文转自:http://my.oschina.net/xishuixixia/blog/132395 常用的调优参数. 1.堆大小 -Xms和-Xmx用于指定堆大小,我们需要将他们俩设置为一样的值,以 ...

  2. 20145316《Java程序设计》第六周学习总结

    20143516许心远 <Java程序设计>第6周学习总结 教材学习内容总结 10.1.1 1.Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 2.若要将数据 ...

  3. springmvc获取bean

    1.普通注解方式获取 2.springcontextholder获取 ChatHistoryService chatHistoryService = SpringContextHolder.getBe ...

  4. ROS学习

    随着机器人领域的快速发展和复杂化,代码的复用性和模块化的需求原来越强烈,而已有的开源机器人系统又不能很好的适应需求.2010年Willow Garage公司发布了开源机器人操作系统ROS(robot ...

  5. SDUT OJ 2607

    /*http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607*/ 题目大意:给出一个字符串,求出里 ...

  6. GoEasyWeb实时推送

    GoEasyWeb实时推送,轻松实现实时消息推送. Web页面订阅(约5行代码),服务器端推送(2行代码)就可以轻松实现,而且在高并发时消息推送稳定. 自己完全可以只花五分钟写出属于自己的第一个实时推 ...

  7. js 判断是ie浏览器

    if (!+[1,]) { //IE window.event.returnValue = false; } else { return false; }

  8. Python-自省机制

     help 如果说能够通过一个函数就能够学会 Python,那这个函数一定就是 Python 提供的第一 个自带说明 help().help 函数的作用就是查看对象的帮组文档.比如: >> ...

  9. # fabirc 配置多组服务器 密码与密钥一起使用 key_filename的设置

    环境说明 myv myv2 是配置在/etc/hosts 的两台 虚拟机 虚拟机ip. 参考英文文档 官方文档的例子不是给的很详细.. http://docs.fabfile.org/en/1.13/ ...

  10. Android -- ContentProvider, 读取和保存系统 联系人

    1. 示例代码 需要的读写联系人的权限 <uses-permission android:name="android.permission.WRITE_CONTACTS"/& ...