题目:

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. Windows 10 1703创意者更新官方ISO镜像大全

    2017年04月07日 20:00 19867 次阅读 稿源:快科技 12 条评论 Windows 10 Creators Update创意者更新正式版已经发布,目前只能通过易生.MCT工具或者ISO ...

  2. GIT简单使用——多人协作篇

    多人协作的工作模式通常是这样:1.首先,可以试图用git push origin <branch-name>推送自己的修改:2.如果推送失败,则因为远程分支比你的本地更新,需要先用git ...

  3. MagicalRecord使用教程【转载】

    原文地址:http://www.ithao123.cn/content-96403.html 下面是在xcode5.1下ARC环境中的使用教程 1. 将 MagicalRecord 文件夹拖入到工程文 ...

  4. HDOJ 题目3564 Another LIS(线段树单点更新,LIS)

    Another LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. bash学习记录

    bash: 管理员:  提示符# 普通用户:提示符$ 环境变量 A=3(变量是指内存空间,A指的是内存空间的名称-变量标示符) PS1  \u@\h:\w\$  \u用户名 \h主机名 \w工作目录的 ...

  7. 【BZOJ2229】[Zjoi2011]最小割 最小割树

    [BZOJ2229][Zjoi2011]最小割 Description 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有 ...

  8. 九度OJ 1003:A+B

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15078 解决:6299 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计 ...

  9. php函数: set_include_path

    <?php $p =get_include_path(); $p.=PATH_SEPARATOR.'./bp/'; $p.=PATH_SEPARATOR.'./CLI/'; $p.=PATH_S ...

  10. [容易]在O(1)时间复杂度删除链表节点

    题目来源:http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/