LintCode "Search a 2D Matrix II"
2015-09-23 05:19
357 查看
Simply a revise of a genius Greedy algorithm seen on LeetCode - linear walking.
class Solution { public: /** * @param matrix: A list of lists of integers * @param target: An integer you want to search in matrix * @return: An integer indicate the total occurrence of target in the given matrix */ int searchMatrix(vector<vector<int> > &m, int target) { int cnt = 0; int h = m.size(); if (!h) return cnt; int w = m[0].size(); int x = w - 1, y = 0; while( (x >= 0 && x < w) && (y >= 0 && y < h)) { int v = m[y][x]; if (v <= target) { cnt += v == target; y ++; } else { x --; } } return cnt; } };
相关文章推荐
- LeetCode-Partition List
- Maximal Square
- Download google drive public shared file in terminal
- Android Api Demos登顶之路(七十七)Graphics-->FingerPaint
- LintCode "Submatrix Sum"
- VM11安装Mac OS X 10.10
- [Index]ZigZag Conversion
- 第二次作业——结对项目之需求分析与原型模型设计
- [C/C++基础知识] 面试再谈struct和union大小问题
- [Index]Search in Rotated Sorted Array series
- 《构建之法》阅读反馈
- Cite a Website in Paper 论文中引用网页的格式
- JS监听关闭浏览器事件
- java 单一登录
- Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa
- Number of Digit One(数学找规律)
- Codeforces Round #321 (Div. 2) Kefa and Park 深搜
- Codeforces Round #321 (Div. 2) Kefa and Company 二分
- Codeforces Round #321 (Div. 2) Kefa and First Steps 模拟
- Ugly Number II