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. 3.13. Notepad++中Windows,Unix,Mac三种格式之间的转换

    由于历史原因,导致Windows,Unix/Linux,Mac三者之间,对于文件中所用回车换行符,表示的方法,都不一样. 这就导致了很多人都会遇到回车换行符的困惑,和需要在不同格式间进行转换. 其中, ...

  2. SharePoint Online 切换经典视图

    SharePoint online 默认是现代视图,我们可以通过Powershell命令切换默认视图. 以下,是完成的Powershell命令: # This file uses CSOM. Repl ...

  3. 无耻之徒(美版)第七季/全集Shameless US迅雷下载

    英文全名Shameless (US),第7季(2016).本季看点:<无耻之徒>(Shameless)第七季.本季故事起始于「一个月之后」,Frank从昏迷中醒来后得知亲人背叛了他,于是向 ...

  4. Android获取actionbar高度和StatusBar高度的方法

    ActionBar: getActionBar().getHeight(); StatusBar: /** * 获取状态栏高度 * * @return */ public static int get ...

  5. 解决 Attempting to destroy the window while drawing!

    当对Dialog进行关闭时,如果有大量的操作,比如动画.绘图什么的,就可能出现这样的错误 Attempting to destroy the window while drawing! 比如,我在自定 ...

  6. 《Google Glass开发指南》

    <Google Glass开发指南> 基本信息 作者: BestApp工作室 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115349477 上架时间:2014-3-19 ...

  7. npm ERR! Error extracting ~/.npm/cloudant/1.9.0/package.tgz archive: ENOENT: no such file or directory, open '~/.npm/cloudant/1.9.0/package.tgz'

    修改package.json Thanks machines returning the above error when , just and now all the builds are pass ...

  8. SecureCRT SSH 语法高亮

    主要原因 1.term类型不对,不支持彩色.在secureCRT上设置 Options->SessionOptions ->Emulation,然后把Terminal类型改成xterm,并 ...

  9. 统计学中RR OR AR HR的区别

    一.相对危险度(RR)——队列研究中分析暴露因素与发病的关联程度 队列研究是选择暴露及未暴露于某一因素的两组人群,追踪其各自的发病结局,比较两组发病结局的差异,从而判定暴露因素与疾病有无关联及关联大小 ...

  10. [转]Windows 下 Apache Virtual hosts 简单配置

    From : http://blog.csdn.net/wuerping/article/details/4164362 /* Author : Andrew.Wu [ Created on : 20 ...