Leetcode: Search a 2D Matrix
2013-10-17 06:58
531 查看
http://oj.leetcode.com/problems/search-a-2d-matrix/
Take the matrix as a sorted list, and then use binary search directly.
Take the matrix as a sorted list, and then use binary search directly.
class Solution { public: int GetValue(vector<vector<int> > &matrix, int index){ int col=matrix[0].size(); int c=index%col; int r=index/col; return matrix[r][c]; } bool searchMatrix(vector<vector<int> > &matrix, int target) { if(matrix.size()==0) return false; int row=matrix.size(), col=matrix[0].size(); int left=0, right=row*col-1; while(left<=right){ int mid=(left+right)/2; int value=GetValue(matrix,mid); if(value==target) return true; else if(value<target) left=mid+1; else right=mid-1; } return false; } };
相关文章推荐
- Leetcode: Container With Most Water
- Leetcode: Rotate Image
- Leetcode: Minimum Path Sum
- Leetcode: Generate Parentheses
- 第19章 导航和布局设计模式
- 百度BAE数据库连接问题
- 百度BAE数据库连接问题
- sizeof运算符的学习笔记
- 多列分区
- g77 arm 编译器
- ORA-01791:不是 SELECTed 表达式
- PHP 中变量的间接引用
- Android基站定位
- JavaScript<四>_DOM
- 线程并发库<二>_线程池
- Combination Sum
- struts2的工作流程(易懂版)
- 白首方恨读书迟
- 解决EF一对一或多对一的删除
- java 关于操作oracle的clob类型