28. Search a 2D Matrix 【easy】

Write an efficient algorithm that searches for a value in an mn matrix.

This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.
Example

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

Challenge

O(log(n) + log(m)) time

解法一:

 class Solution {
public:
/*
* @param matrix: matrix, a list of lists of integers
* @param target: An integer
* @return: a boolean, indicate whether matrix contains target
*/
bool searchMatrix(vector<vector<int>> &matrix, int target) {
int row = matrix.size();
if (row == ) {
return false;
} int col = matrix[].size();
if (col == ) {
return false;
} int start = ;
int end = row * col - ; while (start + < end) {
int mid = start + (end - start) / ; if (matrix[mid / col][mid % col] == target) {
return true;
}
else if (matrix[mid / col][mid % col] < target) {
start = mid;
}
else if (matrix[mid / col][mid % col] > target) {
end = mid;
}
} if (matrix[start / col][start % col] == target
|| matrix[end / col][end % col] == target) {
return true;
}
else {
return false;
}
}
};

注意:

1、边界值合法性检查

2、二维数组转一维数组的计算方式

解法二:

 public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == ) {
return false;
}
if (matrix[] == null || matrix[].length == ) {
return false;
} int row = matrix.length;
int column = matrix[].length; // find the row index, the last number <= target
int start = , end = row - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (matrix[mid][] == target) {
return true;
} else if (matrix[mid][] < target) {
start = mid;
} else {
end = mid;
}
}
if (matrix[end][] <= target) {
row = end;
} else if (matrix[start][] <= target) {
row = start;
} else {
return false;
} // find the column index, the number equal to target
start = ;
end = column - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (matrix[row][mid] == target) {
return true;
} else if (matrix[row][mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (matrix[row][start] == target) {
return true;
} else if (matrix[row][end] == target) {
return true;
}
return false;
}
}

两次二分,值得借鉴!

28. Search a 2D Matrix 【easy】的更多相关文章

  1. Search a 2D Matrix【python】

    class Solution: # @param matrix, a list of lists of integers # @param target, an integer # @return a ...

  2. 【leetcode】Search a 2D Matrix

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  3. 【LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  4. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  6. 【刷题-LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  7. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  8. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

  9. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

随机推荐

  1. POJ 3532 Resistance(高斯消元+基尔霍夫定理)

    [题目链接] http://poj.org/problem?id=3532 [题目大意] 给出n个点,一些点之间有电阻相连,求1~n的等效电阻 [题解] 有基尔霍夫定理:任何一个点(除起点和终点)发出 ...

  2. AOJ 2251 Merry Christmas (最小点覆盖)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2251 [题目大意] 给出一张图,现在有一些任务,要求在ti时刻送礼物 ...

  3. 【块状链表】AutSky_JadeK的块状链表模板+总结(STL版)

    Part 1.块状链表.   定位 插入 删除 数组 O(1) O(n) O(n) 链表 O(n) O(1) O(1) 对于线性表的以上常见操作来说,数组和链表都无法有效地解决.但是,若我们将链表的每 ...

  4. jvm-监控指令-jstat

    格式: jstat -<option> <vmid> [<interval> [<count>]] 作用: 查看虚拟机各种运行状态信息.         ...

  5. S3C2440时钟配置

    参考: http://blog.csdn.net/mr_raptor/article/details/6555734 http://blog.csdn.net/mjx91282041/article/ ...

  6. 【java】处理时间字段 在数据库查询的时候只想要年月日,不想要时分秒 ,java中设置时间类型为年月日,java中设置Date中的时分秒为00.00.000

    JDK8 中最简单的处理方式: @Test public void dateTest(){ Date now = new Date(); System.out.println(now); // jav ...

  7. java 的 CopyOnWriteArrayList类

    初识CopyOnWriteArrayList 第一次见到CopyOnWriteArrayList,是在研究JDBC的时候,每一个数据库的Driver都是维护在一个CopyOnWriteArrayLis ...

  8. Python扫描指定文件夹下(包含子文件夹)的文件

    扫描指定文件夹下的文件.或者匹配指定后缀和前缀的函数. 假设要扫描指定文件夹下的文件,包含子文件夹,调用scan_files("/export/home/test/") 假设要扫描 ...

  9. Java笔记1:IntelliJ IDEA详细安装步骤

    安装IntelliJ IDEA 一.安装JDK 1 下载最新的jdk,这里下的是jdk-8u66 2 将jdk安装到默认的路径C:\Program Files\Java目录下 二.安装IntelliJ ...

  10. Android TextView 常见问题与使用总结

    一.文字显示行数设置 1. 仅显示一行文字 android:singleLine="true" setTransformationMethod(TransformationMeth ...