Search a 2D Matrix II
2015-08-22 15:52
330 查看
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:
Given target = 5, return true.
Given target = 20, return false.
从左上角开始,大于target向前,小于向下。
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.
从左上角开始,大于target向前,小于向下。
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix==null||matrix.length<=0) return false; int m=matrix.length; int n=matrix[0].length; int i=0,j=n-1; while(i<m&&j>=0){ int tmp=matrix[i][j]; if(tmp>target) j--; else if(tmp<target) i++; else return true; } return false; } }
相关文章推荐
- SBJson的使用
- UIButton(继承自UIControl)
- UVA 10970 第一次比赛 D题 (后面才补的)
- redis数据存储系统原理
- s3c6410移植android-kernel-2.6.35内核不识别DM9000网卡
- idea 导入 cordova 生成项目报错 Unable to execute DX
- swing和java里嵌入浏览器
- oc中的文件
- 剑指offer面试题12-打印1到最大的n位数
- Ecshop、Discuz! 等开源产品的局限
- UITextField
- 湖南省第十届大学生计算机程序设计竞赛1511: 残缺的棋盘
- poj2280--Amphiphilic Carbon Molecules(扫描线+极角排序+转换坐标)
- TCP协议中的三次握手及四次握手图解
- RPC提高
- POJ3278Catch That Cow简单一维广搜
- Hibernate学习笔记
- android 判断 activity 状态
- 模式设计的秘密之模板方法模式
- C++打印地址