240. Search a 2D Matrix II
2016-07-30 01:02
281 查看
discuss里面的方法,最先把坐标放在格子的右上角。
如果当前格子比target小,那么就把col往左移动,因为已经是排好序了的,所有右边的都比它大
如果当前格子比target大,就把row往下移,因为上面行的都比它小
否则(即相等),就返回true;
如果当前格子比target小,那么就把col往左移动,因为已经是排好序了的,所有右边的都比它大
如果当前格子比target大,就把row往下移,因为上面行的都比它小
否则(即相等),就返回true;
public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0 || matrix[0].length == 0) { return false; } int row = 0; int col = matrix[0].length - 1; while(row < matrix.length && col >= 0) { if(target == matrix[row][col]) { return true; } else if(target < matrix[row][col]) { col--; } else { row++; } } return false; }
相关文章推荐
- maven学习和搭建环境
- svo的Supplementary matterial 推导过程
- UVA:11624 Fire!(双BFS+打表)
- HDU 4419-矩形面积并+容斥原理
- 批处理集锦——(2)自定义函数
- LintCode:删除排序数组中的重复数字
- 关于kali2.0
- android控件架构、自定义View、ViewGroup
- 代码之美_感悟
- MongoDB 安装配置
- 如何看一个网站是不是.net的几种途径
- 高效并发-Java内存模型和线程
- mysql-5.7.13-win32 安装
- 7/29 CodeForces Div2
- Android Studio 使用Eclipse中的keystore为App签名
- 基于spring-cloud实现eureka注册服务小案例
- C# Winform程序 使用登录窗体到主窗体 且传登录信息
- char* 与 烫烫烫烫烫
- LDA算法调研报告
- java设计模式之单例模式