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 ...
随机推荐
- 【转发】linux yum命令详解
linux yum命令详解 yum(全 称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理, ...
- android studio只能全部提示设置
- Android布局---相对布局
Android布局分为五大类:相对布局.线性布局.表格布局.帧布局.网格布局 相对布局 语法格式: <RelativeLayout xmlns:android="http://sche ...
- C++11 不抛异常的new operator
在google cpp style guide里面明确指出:we don't use exceptions C++11的noexcept关键字为这种选择提供了便利. C++11以前,提及malloc和 ...
- What is the difference Apache (Http Server) and Tomcat (Servlet Container)
The Apache Project The Apache Project is a collaborative software development effort. Its goal is to ...
- 又见蒙特卡洛——python模拟解决三门问题
三门问题很有意思,wiki用不同方法将原理讲的很透彻了,我跟喜欢其中这种理解方式:无论参赛者开始的选择如何,在被主持人问到是否更换时都选择更换.如果参赛者先选中山羊,换之后百分之百赢:如果参赛者先选中 ...
- Canvas绘图API
Canvas就是一个画布,可以进行任何的线.图形.填充等一系列的操作. Canvas的Context对象 要使用Canvas来绘制图形必须在页面中添加Canvas的标签 <canvas id=& ...
- Windows Phone 8.1 Page transitions
original: http://www.visuallylocated.com/post/2014/06/24/Page-transitions-and-animations-in-Windows- ...
- magento -- 给后台分类管理页的分类商品加一栏商品类型
当使用特定分类来控制前台的商品显示时,后台分类管理页的分类商品只有编号.名称.SKU和价格这几栏,选择特定商品相当不便. 可以在这里多加一栏商品类型用来筛选商品,添加的方式很简单. 打开文件/app/ ...
- magento的robots文件编写和判断是否是一个导航分类页面
magento是网店系统,我们突出的是我们的产品,所以,有很多路径我们不想让搜索引擎索引到,所以我们需要用robots文件进行限制 下面是麦神magento的robots.txt里面的内容,因为很多u ...