lintcode ----搜索二维矩阵
来源:互联网 发布:淘宝店铺装修背景图 编辑:程序博客网 时间:2024/05/16 16:20
法一: 右上角开始判断,小于target下移,大于target左移(37ms)
int searchMatrix(vector<vector<int> > &matrix, int target) { // write your code here if(matrix.empty()) return 0; int rows= matrix.size(); int col = matrix[0].size(); int i=0,j=col-1,count=0; while(i<rows&&j>=0) { if(target==matrix[i][j]) { count++; if(i+1<rows) i++; else if(j-1>=0) j--; else return count; } else if(target>matrix[i][j]) i++; else j--; } return count; }
法二:借用map,记录数字出现次数,返回target的次数(50ms)
int searchMatrix(vector<vector<int> > &matrix, int target) { // write your code here unordered_map<int,int> res; for(int i=0;i<matrix.size();i++) { for(int j=0;j<matrix[i].size();j++) { res[matrix[i][j]]++; } } return res[target]; }
0 0
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 【LintCode】搜索二维矩阵
- lintcode,搜索二维矩阵
- LintCode : 搜索二维矩阵
- Lintcode:搜索二维矩阵
- LintCode:搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- 【LintCode】搜索二维矩阵 II
- lintcode,搜索二维矩阵 II
- Lintcode 搜索二维矩阵 II
- LintCode 28 搜索二维矩阵
- [LintCode]28.搜索二维矩阵
- lintcode---搜索二维矩阵Ⅱ
- lintcode-搜索二维矩阵II-38
- 搜索二维矩阵——LintCode
- [LintCode]搜索二维矩阵 II
- LintCode 38 搜索二维矩阵 II
- dell服务器服务器数据丢失后,数据恢复
- Log4j ConversionPattern参数的格式含义
- [动态规划]飞机炸弹游戏 C++实现
- 在Ubuntu 16.04 LTS上搭建LAMP
- 删除SVN版本信息 .svn文件夹
- lintcode ----搜索二维矩阵
- Bat命令学习
- 【Java TCP/IP Socket】UDP Socket(含代码)
- java中将文件打成压缩包工具类filetozip
- 通用应用编译出错
- 第一章 Introduction exercise 1-12
- 最近项目用到Dubbo框架,临时抱佛脚分享一下共探讨。
- 史上最易懂的Android jni开发资料--NDK环境搭建
- form表单加js或ajax验证