lintcode-搜索二维矩阵II-38
来源:互联网 发布:皇家猫粮 知乎 编辑:程序博客网 时间:2024/05/29 13:14
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每一列的整数从上到下是排序的。
- 在每一行或每一列中没有重复的整数。
样例
挑战 要求O(m+n) 时间复杂度和O(1) 额外空间 考虑下列矩阵:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
给出target = 3,返回 2
//遵循杨氏矩阵的搜索规律,从左上角开始,target小于矩阵中该点的值,往左走,大于则往下走
class Solution {public: int searchMatrix(vector<vector<int> > &matrix, int target) { if(matrix.empty()) return 0; int row=matrix.size(); int cal=matrix[0].size(); int i=0,j=cal-1,count=0; while(i>=0&&i<row&&j>=0&&j<cal){ if(target==matrix[i][j]){ ++count; if(i+1<row) ++i; else if(j-1>=0) --j; else return count; }else if(target>matrix[i][j]){ ++i; }else{ --j; } } return count; }};
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
- mysql数据库企业级备份
- android应用分析
- xpath- 一种xml解析器
- 用jQuery和PHP来实现微信转盘抽奖程序 附源代码
- 中文分词与停用词的作用
- lintcode-搜索二维矩阵II-38
- 图形绘制之——Canvas详解(二)-Path路径使用
- Struts 2 – Include multiple Struts configuration files
- PAT(1004) 成绩排名(20)
- ZigZag Conversion
- 浅谈Weka
- 前端文章
- 性能优化_JS篇
- Android 之 Handler 的使用(一)