Java实现-搜索二维矩阵II
来源:互联网 发布:淘宝装修怎么弄全屏 编辑:程序博客网 时间:2024/06/06 14:07
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每一列的整数从上到下是排序的。
- 在每一行或每一列中没有重复的整数。
样例
考虑下列矩阵:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
给出target = 3,返回 2
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 row=matrix.length;int column=matrix[0].length;int count=0;for(int i=0;i<row;i++){for(int j=column-1;j>=0;j--){if(matrix[i][j]==target){column=j;count++;break;}}}return count; }}
阅读全文
0 0
- Java实现-搜索二维矩阵II
- Java实现-搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵 II
- 搜索二维矩阵 II
- 搜索二维矩阵II
- 搜索二维矩阵 II
- 搜索二维矩阵II
- 搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- 【LintCode】搜索二维矩阵 II
- lintcode,搜索二维矩阵 II
- Lintcode 搜索二维矩阵 II
- lintcode-搜索二维矩阵II-38
- [LintCode]搜索二维矩阵 II
- LintCode 38 搜索二维矩阵 II
- [LintCode]38.搜索二维矩阵 II
- 【Twitter Storm系列】flume+Kafka+Storm+HDFS 实时系统搭建
- 今天的收获
- Android ContentProvider
- git pull :因为文件冲突pull失败
- PHP实现异步调用方法研究与分享
- Java实现-搜索二维矩阵II
- python----socket(一)
- 关于LVM管理PVcreate过程中错误Device /dev/sdb not found (or ignored by filtering)
- c++string的深拷贝
- Java实现-翻转链表2
- 2017年,该如何选择容器编排方案? swarm/kubernetes/Mesos ?
- 帮助Bsny
- hihocoder1133(二分求第k小)
- Redis 第一次安装配置,使用