一开始的超时思路

int row=a.size(),col=a[0].size();
for(int i=0;i<row;i++)
{
if(a[i][col-1] > target && a[i][0]<=target)
{
int low=0,high=col-1;
while(low<=high)
{
int mid=(low+high)/2;
if (a[i][mid] > target)
low = mid-1;
else if (a[i][mid] < target)
high = mid + 1;
else
return true;
}
}
}
return false;

 先判断列上的数,是否大于target,改进后还是超时

int row=a.size(),col=a[0].size();
for(int i=0;i<col;i++)
{
if(a[0][i]>target)
{
col=i-1;
break;
}
}
if(col == -1)
return false;
for(int i=0;i<row;i++)
{
if(a[i][col-1] > target && a[i][0]<=target)
{
int low=0,high=col-1;
while(low<=high)
{
int mid=(low+high)/2;
if (a[i][mid] > target)
low = mid-1;
else if (a[i][mid] < target)
high = mid + 1;
else
return true;
}
}
}
return false;

 提示用分治算法,什么是分治?

下面是分治的思路:

从右上角开始查找,为什么我一开始觉得这个思路没有前一个有效率呢?直观上来看 不是很慢吗?

class Solution {
public:
bool searchMatrix(vector<vector<int>>& a, int target) {
int row=a.size(),col=a[0].size();
int i=0,j=col-1;
while(i<row && j>=0)
{
if(a[i][j] > target)
j--;
else if(a[i][j] < target)
i++;
else
return true;
}
return false;
}
};

  

LeetCode() Search a 2D MatrixII的更多相关文章

  1. [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 ...

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

  3. LeetCode Search a 2D Matrix II

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

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

  5. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

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

  7. [leetcode]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. linux的简单网络配置

    1,修改IP edit file: # if rh family system /etc/sysconfig/network-scripts/ifcfg-eth0 (eth0可能会是别的名字) # i ...

  2. GPT分区基础知识及如何在GPT分区上安装WIN7

    大硬盘和WIN8系统,让我们从传统的BIOS+MBR模式升级到UEFI+GPT模式,现在购买的主流电脑,都是预装WIN8系统,为了更好的支持2TB硬盘,更快速的启动win8,预装系统都采取了GPT分区 ...

  3. APP store 官方统计工具的常见的Q&A

    Apple最近在iTunesConnect里最新发布了官方统计工具,提供了现有友盟统计平台和自有统计平台无法统计的数据,具有自己的独有特点,尤其是下面几个最让人头疼的流量分析转化,可以在App Ana ...

  4. JavaScript:变量对象(Variable Object)

    引言:在使用JavaScript编程的时候,避免不了声明函数和变量,但是我们很少知道解释器是如何并且在什么地方找到这些函数和变量的,我们在引用这些对象的时候究竟发生了什么? 对ECMAScript程序 ...

  5. 登陆sqlserver及修改端口号 (转)

    在一台计算机上面同时安装两个sql server数据库实例,第一次安装默认为机器名,端口号为1433 1.如果不知道服务器名,却想登陆的话可以直接输入127.0.0.1登陆之后,在新建查询中输入:SE ...

  6. Apache虚拟目录(二)

    一.PHP生命周期 二.轻量级的PHP 轻量级PHP产品由lighttpd,nginx等等 Apache是基于模块化设计的 了解Apache源代码可以从main.c开始 操作系统上跑了APR运行库 m ...

  7. hdu3911 线段树 区间合并

    //Accepted 3911 750MS 9872K //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

  8. poj2955 区间dp

    //Accepted 200 KB 63 ms //区间dp //dp[i][j] 从i位到j位能得到的最大匹配数 //dp[i][j]=max(dp[i+1][j-1] (s[i-1]==s[j-1 ...

  9. JS 中的五个假值

    1."", undefined, null, 0, NaN 除了这五个假值以外,其他所有值转布尔类型都是true.还有一个特殊的false.

  10. objectARX 获取ucs的X方向

    struct resbuf var; acedGetVar(_T("UCSXDIR"), &var);//获取用户坐标系下X方向量 ver = asVec3d(var.re ...