LeetCode 之 Search a 2D Matrix II
来源:互联网 发布:千里江山图 知乎 编辑:程序博客网 时间:2024/05/18 21:40
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.
For example,
Consider the following 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]]
Given target = 5
, return true
.
Given target = 20
, return false
.
Subscribe to see which companies asked this question
代码如下:
class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int row=matrix.size(); int col=matrix[0].size(); int _row=0,_col=col-1; while(_row<row&&_col>=0){ if(matrix[_row][_col]==target) return true; else if(matrix[_row][_col]>target) _col--; else _row++; } return false; }};
0 0
- leetcode之Search a 2D Matrix II
- LeetCode 之 Search a 2D Matrix II
- #leetcode#Search a 2D Matrix II
- [LeetCode] Search a 2D Matrix II
- leetcode: Search a 2D Matrix II
- leetcode - Search a 2D Matrix II
- [leetcode] Search a 2D Matrix II
- 【leetcode】Search a 2D Matrix II
- Leetcode: Search a 2D Matrix II
- LeetCode Search a 2D Matrix II
- [Leetcode]Search a 2D Matrix II
- [LeetCode]Search a 2D Matrix II
- LeetCode Search a 2D Matrix II
- [LeetCode]Search a 2D Matrix II
- LeetCode || Search a 2D Matrix II
- LeetCode----Search a 2D Matrix II
- Leetcode: Search a 2D Matrix II
- *LeetCode-Search a 2D Matrix II
- linux命令学习一 :touch和mkdir 创建文件和文件夹
- 在Win32下搭建opengl绘制环境
- Scrapy - 爬虫框架
- JAVA_WEB学习经验总结
- Java day04 静态static,静态代码块
- LeetCode 之 Search a 2D Matrix II
- Mybatis 分页插件 3.3.0 发布
- 关于运行报错
- android 从网络加载图片并显示
- JS省份和城市选择控件
- 深入了解MyBatis二级缓存
- 礼拜四log~html form标签 & @ModelAttribute & 字符串判断相等 & DAO文件
- AJax第一个小实例--初学者
- android事件拦截处理机制详解