240. Search a 2D Matrix II
来源:互联网 发布:用服务器ip做网站域名 编辑:程序博客网 时间:2024/05/20 20:47
/*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.由于每一行从左到右、每一列从上到下都是升序,我们可以从矩阵的第一行最右侧开始查找,如果matrix[row][column]==target则返回true;如果matrix[row][column]<target,则说明这一行的所有数都小于target,则直接转到下一行;如果matrix[row][column]>target,则说明这一列所有的数都大于target,则向左移动一列。*/class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { if(matrix.size()==0 || matrix[0].size()==0) return false; int row=0,column=matrix[0].size()-1; while(row<matrix.size() && column>=0) { if(target == matrix[row][column]) return true; else if(target > matrix[row][column]) row++; else column--; } return false; }};
阅读全文
0 0
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240. Search a 2D Matrix II
- 240.Search a 2D Matrix II
- Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图
- spark dataframe创建和操作
- 狗窝里的小日子- 4 ...
- Python中数组的几种使用方法
- C++ 构造析构函数
- 240. Search a 2D Matrix II
- 前端分页
- 机器人如何根据它所看到的找到位置
- 网站SEO优化之图片优化方法
- QModelIndex/Role/Model介紹 二
- AlertDialog弹出框android
- opencv3.3+vs2017
- Android业务组件化之Gradle和Sonatype Nexus搭建私有maven仓库
- AC自动机小结 (HDU 2222,HDU 2896,HDU 3065)