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. [BZOJ5465][APIO2018]选圆圈(KD-Tree)

    题意:给你n个圆,每次选择半径最大的,将它和与它相交的圆全部删去,输出每个圆是在哪次被删的. KD树模板题.用一个矩形框住这个圆,就可以直接剪枝了.为了防止被卡可以将点旋转一个角度,为了保险还可以多转 ...

  2. [BZOJ4897][THUSC2016]成绩单(DP)

    4897: [Thu Summer Camp2016]成绩单 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 220  Solved: 132[Subm ...

  3. Codechef ForbiddenSum

    Mike likes to invent new functions. The latest one he has invented is called ForbiddenSum. Let's con ...

  4. 【贪心】Gym - 100507H - Pair: normal and paranormal

    每次取相邻的两个可以射击的从序列中删除,重复n次. 可以看作括号序列的匹配. #include<cstdio> #include<vector> using namespace ...

  5. 【找规律】Gym - 100923L - Por Costel and the Semipalindromes

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

  6. 微服务之SpringCloud实战(一):SpringCloud简介

    什么是微服务架构 微服务架构就是系统架构设计的一种风格,它主旨将一个独立的系统,拆分成各个微服务,各个微服务独立运行,他们之间通过Http的Restful API进行通信,拆分出来的微服务是根据原系统 ...

  7. 生成唯一code

    private String getCode() { List<String> ptypeCodeList = mapper.findCodeList(); String code = & ...

  8. Asp.Net MVC part4 异步、校验、区域Area

    异步方式1:使用jquery的异步函数方式2:使用MVC的AjaxHelper行为的返回值设置:JsonResult对象,使用Json方法接收一个对象,在内部会完成对象的js序列化,向输出流中输出js ...

  9. Android开发工具

    Android开发工具: AndroidDevTools: 收集整理Android开发所需的Android SDK.开发中用到的工具.Android开发教程.Android设计规范,免费的设计素材等. ...

  10. IntelliJ IDEA字符串常量长度太长的问题解决:constant string too long

    Java compiler下的Use compiler为Eclipse: