LintCode:搜索二维矩阵 II
来源:互联网 发布:smartpss监控软件下载 编辑:程序博客网 时间:2024/05/29 15:05
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每一列的整数从上到下是排序的。
- 在每一行或每一列中没有重复的整数。
Yes
样例
考虑下列矩阵:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
给出target = 3,返回 2
挑战
标签 Expand 要求O(m+n) 时间复杂度和O(1) 额外空间
相关题目 Expand
解题思路:
从左下脚开始搜索即可
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 int res = 0; if (null == matrix || 0 == matrix.length) return res; int row = matrix.length; int colum = matrix[0].length; int i = row - 1; int j = 0; while (i >= 0 && j < colum) { if (target == matrix[i][j]) { res++; i--; j++; } else if (target > matrix[i][j]) { j++; } else { i--; } } return res; }}
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
- Linux之命令的组合
- 给Openwrt安装polipo
- 剑指offer笔记(一)
- 显示网络上的图片
- OC基础知识 -- 继承
- LintCode:搜索二维矩阵 II
- iOS实例化plist文件到数组中
- 用typedef定义函数类型
- LintCode:搜索区间
- Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法
- php如何实现ajax分页
- 伺服驱动器与变频器的区别
- 不同平台下StreamingAssets的文件路径预编译
- Docker安装配置使用总结_20151105_莫小贝