题目:

Write an efficient algorithm that searches for a value in an m x n 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.

题解:

Solution 1 ()

class Solution {
public:
/**
* @param 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) {
if (matrix.empty() || matrix[].empty()) {
return false;
} int rowEnd = matrix.size() - ;
int colEnd = matrix[].size() - ;
int start = , mid = ;
//rowMid
while (start + < rowEnd) {
mid = start + (rowEnd - start) / ;
if (matrix[mid][] == target) {
return true;
} else if (matrix[mid][] > target) {
rowEnd = mid;
} else {
start = mid;
}
} int row = ;
if (matrix[start][] <= target && matrix[start][colEnd] >= target) {
row = start;
} else if (matrix[rowEnd][] <= target && matrix[rowEnd][colEnd] >= target) {
row = rowEnd;
} else {
return false;
}
start = ; //colMid
while (start + < colEnd) {
mid = start + (colEnd - start) / ;
if (matrix[row][mid] == target) {
return true;
} else if (matrix[row][mid] > target) {
colEnd = mid;
} else {
start = mid;
}
} if(matrix[row][start] == target || matrix[row][colEnd] == target) {
return true;
} return false;
}
};

Solution 2 ()

class Solution {
public:
/**
* @param 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) {
if (matrix.empty() || matrix[].empty()) {
return false;
} int m = matrix.size(), n = matrix[].size();
int start = ;
int end = m * n - ;
int mid = ; while (start + < end) {
mid = start + (end - start) / ;
int row = mid / n;
int col = mid % n;
if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] > target) {
end = mid;
} else {
start = mid;
}
} if (matrix[start / n][start % n] == target) {
return true;
} else if (matrix[end / n][end % n] == target) {
return true;
} else {
return false;
}
}
};

【Lintcode】028.Search a 2D Matrix的更多相关文章

  1. 【Lintcode】038.Search a 2D Matrix II

    题目: Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence ...

  2. 【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 ...

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

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

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

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

  5. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  6. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

  7. 【刷题-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 ...

  8. 【一天一道LeetCode】#74. Search a 2D Matrix

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  9. 【Lintcode】011.Search Range in Binary Search Tree

    题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find a ...

随机推荐

  1. CentOS6.5下Oracle11G-R2安装、卸载

    CentOS6.5下Oracle11G-R2安装.卸载 资源下载地址(包含本人全部安装过程中,系统备份文件):http://download.csdn.net/detail/attagain/7700 ...

  2. 某个时间段关闭API

    def bet(): if 000000 < int(time.strftime("%H%M%S", time.localtime())) < 90000: print ...

  3. spring boot 发布成包所需插件

    在pom.xml里配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...

  4. HDFS源码分析心跳汇报之整体结构

    我们知道,HDFS全称是Hadoop Distribute FileSystem,即Hadoop分布式文件系统.既然它是一个分布式文件系统,那么肯定存在很多物理节点,而这其中,就会有主从节点之分.在H ...

  5. Linux kernel 2.6下的modules编译与KBuild

    转载:http://blog.sina.com.cn/s/blog_602f87700100dq1u.html Sam之前在Linux kernel 2.4下写过一些driver.但自从转到kerne ...

  6. IntelliJ IDEA生成 Serializable 序列化 UID 的快捷键

    首先创建一个类如Movie,让该类实现Serializable序列化接口. 然后我们需要依次按照以下的方法找到 Settings 之后我们需要以下几个操作,并找到 Serializable class ...

  7. load-on-startup 解释

    <!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  &qu ...

  8. 一步一步 在mac上安装ubuntu

    做为程序猿的你,一定听说过Linux甚至很喜欢Linux. 近期买了一台mac air,我很喜欢苹果的工艺,但作为屌丝程序猿,我依然喜欢基于Linux内核的Ubuntu 进行开发.以下我就讲述一步一步 ...

  9. programming review (c++): (3)graph, binary search

    I.graph #include <iostream> #include <vector> using namespace std; vector<vector<, ...

  10. HNOI2016

    本蒟蒻表示终于$AC$了$HNOI2016$的六道毒瘤题... 高兴! 附上各个题的题解: $DAY1$: $T1$: BZOJ4537: [Hnoi2016]最小公倍数 $T2$: BZOJ4538 ...