leetcode Search a 2D Matrix
2015-12-09 20:40
295 查看
原题链接:https://leetcode.com/problems/search-a-2d-matrix/
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
Description
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 from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { if (matrix.empty() || matrix[0].empty()) return false; n = matrix.size(); for (int i = 0; i < n; i++) { It p = lower_bound(matrix[i].begin(), matrix[i].end(), target); if (p != matrix[i].end() && *p == target) return true; } return false; } private: int n; typedef vector<int>::iterator It; };
相关文章推荐
- Qt学习小记-NULL字符串和空(empty)字符串的区别
- javaEE中的@WebServlet的使用
- Matlab的mapminmax函数说明
- NSUserDefaults 数据持久化
- c++
- C#调用WINDOWS命令行(CMD)
- 工作调度---at
- c# 引用外部dll
- windows安装python问题
- VC++ 最小化到托盘、恢复
- WPF BitmapImage 占用资源无法释放、无法删除的问题
- 渠道快速接入-U8Server-U8SDK
- 固定显示
- 最大子矩阵 DP
- Linux对于录音
- 召回2014年8英语个月的道路
- 图形界面汇总
- Android设计模式(十)--生成器模式
- HDU4870:Rating(DP)
- TCP/IP-协议族----17、应用层简单