1.

设查找的数位y,第一行最后一列的数位x

如果x<y,x是第一行最大的,所以第一行都小于y,删除第一行;

如果x>y,x是最后一列最小的,所以最后一列都大于y,删除最后一列;

这样保证x永远在可能有解的矩阵的第一行,最后一列。

时间复杂度:O(m+n)

 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) {
// write your code here
int m = matrix.size();
if (m == ) {
return false;
}
int n = matrix[].size();
int r = , c = n - ;
while (r < m && c >= ) {
if (matrix[r][c] < target) {
r++;
} else if (matrix[r][c] > target) {
c--;
} else {
return true;
}
}
return false;
}
};

2. 分治法1

设查找的数位y,取中心点x,把矩阵分解成4部分

如果x<y,x是A中最大的,所以A都小于y,删除A;

如果x>y,x是D中最小的,所以D都小于y,删除D;

A  | B

————

C  | D

时间复杂度:O(N) = O(N/4)+O(N/2)+O(1), 介于O(N^0.5)~O(N)之间

 class Solution {
public:
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
bool helper(vector<vector<int> > &M, int x1, int y1, int x2, int y2, int target) {
if (x1 > x2 || y1 > y2) {//empty matrix
return false;
}
int midx = (x1 + x2)>>;
int midy = (y1 + y2)>>;
if (M[midx][midy] == target) {
return true;
}
return (M[midx][midy] > target)?
(helper(M, x1, y1, x2, midy-, target)||helper(M, x1, midy, midx-, y2, target)):
(helper(M, x1, midy+, x2, y2, target)||helper(M, midx+, y1, x2, midy, target)); }
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// write your code here
int m = matrix.size();
if (m == ) {
return false;
}
int n = matrix[].size();
return helper(matrix, , , m - , n - , target);
}
};

3. 分治法2

设查找的数为y,在中线找到这样两个数x1,x2,使得x1<y,x2>y,把矩阵分成4部分

A|     B

————

C|    D

x1是A中最大的,所以A都小于y,删掉A;

x2是D中最小的,所以D都大于y,删掉D;

时间复杂度:O(N)=2O(N/4)+O(logn), 为O(N^0.5)

 class Solution {
public:
/**
* @param A, a list of integers
* @param left, an integer
* @param right, an integer
* @param target, an integer
* @return an integer, indicate the index of the last number less than or equal to target in A
*/
int binarySearch(vector<int> &A, int left, int right, int target) {
while (left <= right) {//not an empty list
int mid = (left + right) >> ;
if (A[mid] <= target) {
left = mid + ;//left is in the integer after the last integer less than or equal to target
} else {
right = mid - ;
}
}
return left - ;
}
/**
* @param M, a list of lists of integers
* @param x1, an integer
* @param y1, an integer
* @param x2, an integer
* @param y2, an integer
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
bool helper(vector<vector<int> > &M, int x1, int y1, int x2, int y2, int target) {
if (x1 > x2 || y1 > y2) {//empty matrix
return false;
}
int midx = (x1 + x2)>>;
int indy = binarySearch(M[midx], y1, y2, target);
//M[midx][indy] <= target
if ((indy >= y1) && (M[midx][indy] == target)) {
return true;
}
return (helper(M, x1, indy+, midx-, y2, target))||(helper(M, midx+, y1, x2, indy, target)); }
/**
* @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) {
// write your code here
int m = matrix.size();
if (m == ) {
return false;
}
int n = matrix[].size();
return helper(matrix, , , m - , n - , target);
}
};

LintCode: Search A 2d Matrix的更多相关文章

  1. LintCode Search a 2D Matrix II

    排好序的二维数组, 从上到下从左到右增大, 给出一个数找出此数组里有多少个这个数. 不用两个循环做, 着手于条件(从左下角开始,若相等往右上跳一个,若小于target往右边跳一个,若大于target往 ...

  2. LintCode 38. Search a 2D Matrix II

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

  3. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. [LeetCode] Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

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

  6. 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 ...

  7. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  8. 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, ret ...

  9. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

随机推荐

  1. libxml2.dylb 罗致<libxml/tree.h> 老是找不到头文件

    libxml2.dylb 导致<libxml/tree.h> 老是找不到头文件 添加了libxml2.dylb的framework ,结果还是引用不了<libxml/tree.h&g ...

  2. Atlassian JIRA Change IP

    Oracle Linux 6.8 Atalssian JIRA 7 原来IP: 192.168.10.200 改新IP: 192.168.12.200 重新跑应用报错,如下所示: 官方提示应用连接不上 ...

  3. java中Double类数字太大时页面正常显示而不要用科学计数法

    /** * 当浮点型数据位数超过10位之后,数据变成科学计数法显示.用此方法可以使其正常显示. * @param value * @return Sting */ public static Stri ...

  4. 豪斯医生第一季/全集House M.D 1迅雷下载

    豪斯医生 第一季 House M.D. Season 1 (2004)本季看点:态度无礼,表情凶恶,跛足拄着一根藤棍,永远是牛仔裤运动鞋的便装打扮而不是整洁的白大褂,普林斯顿大学附属医院的格雷戈·豪斯 ...

  5. 犯罪现场调查第一季/全集CSI迅雷下载

    英文译名 CSI (第1季) (2000首播)CBS. 本季看点:<犯罪现场调查>赌城拉斯维加斯吸引着做发财梦的人,也吸引着形形色色的罪犯,该市警察局的犯罪现场调查局在全美国名列第二.该剧 ...

  6. java 生成zip文件并导出

    总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...

  7. java转义符和正则表达式转义符

    举例来说,连续相同的3位数字的正则表达式的标准语法是: ([\d])\1{2} 但是如果在java代码中这么写,就会出现语法错误,如下: String regEx = "([\d])\1{2 ...

  8. ASP.NET MVC:WebPageBase.cs

    ylbtech-funcation-Utility: ASP.NET MVC:WebPageBase.cs 充当表示 ASP.NET Razor 页的类的基类. 1.A,WebPageBase 抽象类 ...

  9. Verilog 加法器和减法器(2)

    类似半加器和全加器,也有半减器和全减器. 半减器只考虑当前两位二进制数相减,输出为差以及是否向高位借位,而全减器还要考虑当前位的低位是否曾有借位.它们的真值表如下: 对半减器,diff = x ^y, ...

  10. 第三十一章 elk(2)- 第二种架构(最常用架构)

    参考:http://linuxg.blog.51cto.com/4410110/1761757 最常用架构: 一.安装redis 1.下载:http://redis.io/download 2.解压后 ...