搜索二维矩阵
来源:互联网 发布:52单片机输出电压 编辑:程序博客网 时间:2024/05/17 03:36
容易 搜索二维矩阵
27%
通过
写出一个高效的算法来搜索 m × n矩阵中的值。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每行的第一个数大于上一行的最后一个整数。
Yes
样例
考虑下列矩阵:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
给出 target = 3
,返回 true
挑战
O(log(n) + log(m)) 时间复杂度
public class Solution { /** * @param 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 int m=-1; int lan = matrix.length; if(lan == 0) return false; int len = matrix[0].length; for(int i=0;i<lan;i++) { if(matrix[i][0]==target) return true; else if(i==lan-1) { m = lan-1; break; } else if(matrix[i][0]<target && matrix[i+1][0]>target) { m = i; break; } } for(int i=0;i<len;i++) { if(matrix[m][i]==target) return true; }return false; }}
0 0
- 题目:搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵 II
- 搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 搜索二维矩阵II
- 【LintCode】搜索二维矩阵
- 搜索二维矩阵
- lintcode,搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵 II
- LintCode : 搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵
- 跨库嵌套子查询和关联子查询
- 遇到一个代码混乱不堪的Android项目该怎么办?
- HTML基础_02_格式标签
- A First Problem : Stable Matching(第一个重要问题:稳定匹配)
- spring mvc的分层思想
- 搜索二维矩阵
- 自适应网页设计(Responsive Web Design)
- shell 清空日志简单例子(三)
- 2013杭州站A - Lights Against Dudely(状态模拟)
- web页面自适应手机屏幕宽度
- [Servlet]Web应用的基础部署
- 关于UITableView及dequeueReusableCellWithIdentifier的一些理解
- 自定义控件的属性
- 手工计算浮点数到16进制科学记数法的辅助验证工具