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. UINavigationController 、UINavigationBar 、UINavigationItem 超清晰直观详解

    UINavigationController 部分 1. UINavigationController 是一个容器类.里面盛放的是UIViewController. 容器的意思是,如果你不放入UIVi ...

  2. c++ strcmp strcpy sprintf

  3. AIDL interface XXX should be declared in a file

    在写AIDL的时候出现了interface XXX should be declared in a file, 错误...经过反复查看,发现AIDL规定,文件名必须和interface XXX名字相同 ...

  4. WordPress主题开发:WP_Query基本用法

    为什么要学WP_Query? wordpress默认会根据网址调用数据,不能满足我们所有建站要求,而WP_Query可以用于查询任何你想要的内容,相当于自定义数据调用. 便于记忆,在讲用法之前我们回顾 ...

  5. SharePoint JavaScript API in application pages

    前言 最近,在SharePoint 应用程序页中写JavaScript API,进行一些数据交互.其实,很简单的事情却遇到了问题,记录一下,希望能对遇到类似问题的人以帮助. 引用JavaScript ...

  6. Swift - 多个mask的动画效果

    Swift - 多个mask的动画效果 效果 源码 https://github.com/YouXianMing/Swift-Animations // // TranformFadeView.swi ...

  7. window.opener()方法

    <!DOCTYPE html><html><head><meta charset="GBK"><title>菜鸟教程(r ...

  8. Python获取数字的二进制值

    目标 想要获取一个整形数字的二进制表示 bin 内置函数 看一下官方的解释 Convert an integer number to a binary string prefixed with &qu ...

  9. NAT模式

    NAT NAT模式中,就是让虚拟机借助NAT(网络地址转换)功能,通过宿主机器所在的网络来访问公网. NAT模式中,虚拟机的网卡和物理网卡的网络,不在同一个网络,虚拟机的网卡,是在vmware提供的一 ...

  10. django的权限认证:登录和退出。auth模块和@login_required装饰器

    在settings.py中配置LOGIN_URL参数: # 用户访问带有(@login_required)标签的页面(view)时,如果没有登录,就会跳转到LOGIN_URL(即登陆url). LOG ...