题目:搜索二维矩阵
来源:互联网 发布:qq弃号扫号软件 编辑:程序博客网 时间:2024/04/30 00:22
写出一个高效的算法来搜索 m × n矩阵中的值。
这个矩阵具有以下特性:
- 每行中的整数从左到右是排序的。
- 每行的第一个数大于上一行的最后一个整数。
您在真实的面试中是否遇到过这个题?
考虑下列矩阵:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
给出 target = 3
,返回 true
O(log(n) + log(m)) 时间复杂度
标签 Expand
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
if (matrix == null || matrix.length == 0) {
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int x = 0;
int y = n - 1;
while (x >= 0 && y >= 0 && x < m && y < n) {
if (matrix[x][y] == target) {
return true;
} else if (matrix[x][y] < target) {
x = x + 1;
} else if (matrix[x][y] > target) {
y = y - 1;
}
}
return false;
}
}
- 题目:搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵 II
- 搜索二维矩阵
- 搜索二维矩阵 II
- 搜索二维矩阵
- lintcode ---- 搜索二维矩阵
- lintcode ----搜索二维矩阵
- 搜索二维矩阵II
- 【LintCode】搜索二维矩阵
- 搜索二维矩阵
- lintcode,搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵 II
- LintCode : 搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵
- 搜索二维矩阵
- Struts(1)初识Struts
- 变量声明和定义的关系
- Orchard CMS 学习日记1
- POJ 2251 Dungeon Master (BFS)
- #开发中注意的问题
- 题目:搜索二维矩阵
- 博客停止维护
- 解决CentOS 6.2下安装ipvsadm-1.26报错
- 题目:搜索插入位置
- 再谈数据结构的重要性
- poj1206(dp)
- http访问
- 数据挖掘之关联分析五(序列模式)
- ubuntu14.04 adb的安装和使用笔记