LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)
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(搜寻二维矩阵)的更多相关文章
- [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 ...
- 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 ...
- 240 Search a 2D Matrix II 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列.例如,考虑下面的矩阵:[ [1, 4, 7 ...
- Leetcode240. Search a 2D Matrix II搜索二维矩阵2
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 matrix ...
- [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 ...
- [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 ...
- [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 ...
- (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 ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- 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 ...
随机推荐
- python3 驱动 PyMySQL
Python版本: 3.5.0 MySqlDB官网只支持Python3.4, 使用第三方库PyMysql连接Mysql数据库. https://pypi.python.org/pypi/PyMySQ ...
- C++ 第三十四天
c++ 已经搁了很久了,之所以捡起来是因为学校数据结构课程设置 **.我对 c++ 的掌握非常不扎实,因为除了顺序阅读 c++ primer 外就没有什么实践, 但是我又无法忍受自己写出来的 * 一样 ...
- 20145327实验三 敏捷开发与XP实践
敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 程序缩进: 结对git的应用 PSP 步骤 耗时 百分比 需求分析 15m 12.5% 设计 30m 25% 代码实现 45m 37.5 ...
- Swift开发之泛型实例
一.Swift泛型 泛型能够让开发者编写自定义需求已经任意类型的灵活可用的的函数和类型.能够让我们避免重复的代码.用一种清晰和抽象的方式来表达代码的意图. func swapTwoStrings(_ ...
- Redis windows主从服务配置
一.下载redis解压 如图: 二.复制redis.windows.conf 文件为 redis.windows_6380.conf 三.修改配置IP和端口 四.配置从属于主服务的IP 和 端口 五. ...
- springboot集成shiro 前后端分离 统一处理shiro异常
在前后端分离的情况下,shiro一些权限异常处理会返回401之类的结果,这种结果不好统一管理.我们希望的结果是统一管理,所有情况都受我们控制 就算权限验证失败,我们也希望返回200,并且返回我们定义的 ...
- C# 删除文件以及文件夹
代码如下: /// <summary> /// 删除文件夹以及文件 /// </summary> /// <param name="directoryPath& ...
- springMVC多视图的支持
1.在springmvc.xml中加上 <!-- 多视图的支持 --> <bean class="org.springframework.web.servlet.view. ...
- 好的Mysql 查询语句
select swr.id,swr.name,swr.sort as type,count(swl.id) as nums,ifnull(sum(swl.package_num),0) package ...
- python 贪婪和非贪婪模式
这样的正则表达式: r'\*(.+)\*' 如果想要匹配*something*这样的一个串按道理说是没问题的 但是如果文本是*this* is *something* 那么我们的正则表达式就会采取贪 ...