搜索二维矩阵 II
2015-10-03 22:36
344 查看
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。
这个矩阵具有以下特性:
每行中的整数从左到右是排序的。
每一列的整数从上到下是排序的。
在每一行或每一列中没有重复的整数。
这个矩阵具有以下特性:
每行中的整数从左到右是排序的。
每一列的整数从上到下是排序的。
在每一行或每一列中没有重复的整数。
public class Solution { /** * @param matrix: A list of lists of integers * @param: A number you want to search in the matrix * @return: An integer indicate the occurrence of target in the given matrix */ public int searchMatrix(int[][] matrix, int target) { // write your code here if(matrix==null || matrix.length==0){ return 0; } int count=0; int row=matrix.length-1; int col=0; while(row>=0 && col<=matrix[0].length-1){ if(target==matrix[row][col]){ count++; row--; col++; }else if(target>matrix[row][col]){ col++; }else{ row--; } } return count; } }
相关文章推荐
- Spring MVC 解读——@RequestMapping
- hdu 1430 魔板
- Hive笔记四:Hive的组件和工作流程
- Spring AOP概念
- SQL 基础
- Spring MVC 解读---<context:component-scan/>
- 网站用户存在状态的session用法及例子
- 为什么还是有很多人都选择NATIVE APP
- 利用树集进行数据排序
- 分词算法的python实现(正向最大匹配法)
- MySQL索引背后的数据结构及算法原理
- eclipse主题颜色配置
- jQuery时间轴插件:jQuery Timelinr
- 数据库之Oracle
- Hive笔记三:基本操作
- NLP学术组织、会与论文
- 算法设计技术
- Linux对rm的误操作预防
- Spring MVC 解读——<mvc:annotation-driven/>
- 安卓控件使用系列32:ViewPager多页面滑动控件的使用方法1