LeetCode() Search a 2D MatrixII
一开始的超时思路
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的更多相关文章
- [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 ...
- [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 ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- 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 ...
- 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 ...
- 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 ...
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [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 ...
- [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 ...
随机推荐
- 基于K2 BPM平台,中原地产实现了从2个人到5万多人的跨越
演讲人:吴付文 中原地产CIO 点击这里查看中原地产怎么使用BPM实现业绩的飞跃式发展.
- 本节向大家介绍一下UML建模误区
本节向大家介绍一下UML建模误区,这里向大家介绍九个误区,希望通过本节的学习,你对UML建模有清晰的认识,以免在以后使用过程中产生不必要的麻烦.下面让我们一起来看一下这些建模误区吧. UML建模误区 ...
- SharePoint 2013 开发——SharePoint APP介绍
博客地址:http://blog.csdn.net/FoxDave 新的APP模型让我们能够创建看起来像是SharePoint的一部分的应用程序,但是它完全运行在独立于SharePoint服务器 ...
- JVM-运行时数据区
运行时数据区示意图 ...
- beanUtil
mvc中,页面传值进来,struts2框架是用modeldriven spingmvc是model 不用框架的话,要手动一个一个的设置,然后在用dao方法与数据库联系 servlet框架有BeanUt ...
- Unichar, char, wchar_t
之前总结了一些关于字符表示,以及字符串的知识. 现在在看看一些关于编译器支持的知识. L"" Prefix 几乎所有的编译器都支持L“” prefix,一个字符串如果带有L“”pr ...
- JavaScript常用检测脚本(正则表达式)
转自:http://www.cnblogs.com/skylaugh/archive/2006/09/25/514492.html 文件名称:check.js 说明:JavaScript脚本,用于检查 ...
- Titanium vs PhoneGap
http://mobile.51cto.com/Titanium-318049.htm http://www.ibm.com/developerworks/cn/opensource/os-titan ...
- hdu 2035
Ps:查了下快速幂,顺便在这用下.... 积的求余等于两个数的求余的积再求余... 代码: #include "stdio.h"int mod(int a,int b);int m ...
- C++ offsetof
这是一个宏,用于计算类中某个成员的地址相对于类实例的偏移量 在C++11中,要求这个类standard_layout 基本用法是这样子的: #include <stdio.h> /* pr ...