LintCode 38 搜索二维矩阵 II
来源:互联网 发布:手机处理数据软件 编辑:程序博客网 时间:2024/05/15 18:22
题目:searchMatrix
要求:
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。
这个矩阵具有以下特性:
每行中的整数从左到右是排序的。
每一列的整数从上到下是排序的。
在每一行或每一列中没有重复的整数。
样例:
考虑下列矩阵:
[
[1, 3, 5, 7],[2, 4, 7, 8],[3, 5, 9, 10]
]
给出target = 3,返回 2
算法要求:
要求O(m+n) 时间复杂度和O(1) 额外空间
解题思路:
根据题中给出的排序状态,就可以进行躲避无用的搜索操作啦。
算法如下:
int searchMatrix(vector<vector<int> > &matrix, int target) { // write your code here int num = 0; int size2 = matrix.size(); if (size2 == 0) { return 0; } int size = matrix[0].size(); for (int i = 0; i < size && matrix[0][i] <= target; i++) { for (int j = 0; j < size2 && matrix[j][i] <= target; j++) { if (matrix[j][i] == target) { num++; } } } return num; }
0 0
- lintcode-搜索二维矩阵II-38
- LintCode 38 搜索二维矩阵 II
- LintCode之38 搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- 【LintCode】搜索二维矩阵 II
- lintcode,搜索二维矩阵 II
- Lintcode 搜索二维矩阵 II
- [LintCode]搜索二维矩阵 II
- [LintCode]38.搜索二维矩阵 II
- Google/LintCode:M-搜索二维矩阵 II
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 【LintCode】搜索二维矩阵
- lintcode,搜索二维矩阵
- LintCode : 搜索二维矩阵
- Lintcode:搜索二维矩阵
- 搜索二维矩阵 II
- [Python爬虫]爬取东方财富网公司公告需要注意的几个问题
- Java8之lambda表达式
- sort命令
- 修改页面滚动条样式CSS代码!(待修改兼容性)
- 分书问题(搜索)
- LintCode 38 搜索二维矩阵 II
- stmp协议以及邮件的发送
- 1649
- ULUA的简洁用法(二)
- Lucene教程--Analyzer分析器详解
- js判断鼠标移入的方向并触发事件
- Golang 学习之路四:类型
- 转:kafka数据可靠性深度解读
- InitInstance中创建CWnd派生的窗口对象,内存泄漏