【LintCode】搜索二维矩阵 II
来源:互联网 发布:二维数组全部初始化为0 编辑:程序博客网 时间:2024/05/29 13:52
描述
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每一列的整数从上到下是排序的。
- 在每一行或每一列中没有重复的整数。
考虑下列矩阵:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
给出target = 3,返回 2
代码<span style="font-size:18px;">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 //从右上角开始搜索,因为从左到右递增,从上到下递增,从右上角 //开始搜索的话效率高,target值大于矩阵点row++,target值小于 //矩阵点,column--,target值等于矩阵点,count++,row++,column-- int count=0; if(matrix.length==0) return 0; int row=matrix.length; int column=matrix[0].length; int i=0; int j=column-1; while(i<=row-1&&j>=0){ if(target>matrix[i][j]) i++; else if(target<matrix[i][j]) j--; else if(target==matrix[i][j]){ count++; i++; j--; } } return count; }}</span>
0 0
- LintCode:搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- 【LintCode】搜索二维矩阵 II
- lintcode,搜索二维矩阵 II
- Lintcode 搜索二维矩阵 II
- lintcode-搜索二维矩阵II-38
- [LintCode]搜索二维矩阵 II
- LintCode 38 搜索二维矩阵 II
- [LintCode]38.搜索二维矩阵 II
- Google/LintCode:M-搜索二维矩阵 II
- LintCode之38 搜索二维矩阵 II
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 【LintCode】搜索二维矩阵
- lintcode,搜索二维矩阵
- LintCode : 搜索二维矩阵
- Lintcode:搜索二维矩阵
- 搜索二维矩阵 II
- sql join 降低笛卡尔积结果集
- CentOS7配置多监听端口与多虚拟主机
- effective stl 第14条:使用reserve来避免不必要的重新分配
- android真机调试一直卡在installing apk
- 11.2 Data Guard 使用增量备份基于最小数据文件头scn的GAP恢复 on AIX 6.1
- 【LintCode】搜索二维矩阵 II
- NOIP模拟题 2016.9.10 [动态规划] [概率] [贪心]
- 全程无死角指导kali安装中文输入法..
- 使用mapreduce实现GA
- Android SpannableString 设置
- 多态的基本知识<多态的前提是继承哟!!!!>
- 安卓官方解决加载图片内存溢出问题
- Quartz与Spring集成——创建调度器
- JAVA 学习模块十三: 接口