(lintcode)第28题 搜索二维矩阵
来源:互联网 发布:云计算厂商 编辑:程序博客网 时间:2024/05/20 09:22
写出一个高效的算法来搜索 m × n矩阵中的值。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每行的第一个数大于上一行的最后一个整数。
样例思路比较简单,和蛮力破解没有什么不一样,在这里就不说了,直接贴代码。
考虑下列矩阵:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
给出 target = 3
,返回 true
代码如下:
public class Solution { /* * @param matrix: matrix, a list of lists of integers * @param target: An integer * @return: a boolean, indicate whether matrix contains target */ public boolean searchMatrix(int[][] matrix, int target) { // write your code here for(int i=0;i<matrix.length;i++){ if(matrix[i][matrix[i].length-1]<target){ continue;//直接跳到下一行 }else if(matrix[i][matrix[i].length-1]>=target){//在该行搜索 for(int j=0;j<matrix[i].length;j++){ if(matrix[i][j]==target) return true; } return false;//没有搜索到 } } return false; }}
阅读全文
0 0
- (lintcode)第28题 搜索二维矩阵
- LintCode 28 搜索二维矩阵
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 【LintCode】搜索二维矩阵
- lintcode,搜索二维矩阵
- LintCode : 搜索二维矩阵
- Lintcode:搜索二维矩阵
- LintCode之28 搜索二维矩阵
- LintCode:搜索二维矩阵 II
- LintCode:搜索二维矩阵 II
- 【LintCode】搜索二维矩阵 II
- lintcode,搜索二维矩阵 II
- Lintcode 搜索二维矩阵 II
- [LintCode]28.搜索二维矩阵
- lintcode---搜索二维矩阵Ⅱ
- lintcode-搜索二维矩阵II-38
- 搜索二维矩阵——LintCode
- 入门赛6
- SVN怎么查看修改的历史记录并做比较
- Lua_文件I/O_012
- 考研路茫茫——空调教室【双联通分量+树形DP】
- 【java】调用微信模板消息推送,线程池优化推送速度。
- (lintcode)第28题 搜索二维矩阵
- MYSQL优化(一):【架构解析、sql语句执行详细流程】
- Linux误删除oracle_home根目录解决方案:
- Servlet/jsp之监听器
- 使用Fiddler的AutoResponder转发请求
- 生产环境下JAVA进程高CPU占用故障排查
- 【jvm】堆外内存
- 安装svn
- 实训第一课