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认证考试,为竞争力加分
思科Cisco认证.ADOBE认证.微软认证.印度AIIT认证.华为认证.IBM认证等……在各种“证”满天飞的时候,如何独具慧眼选择最有含金量的证书? 八成 的人力资源主管会检查求职者的认证情况.(C ...
- fastboot 刷system.img 提示 sending 'system' (*KB)... FAILED (remote: data too large)
华为G6-C00卡刷提示OEMSBL错误,只能线刷 ,但是官方找不到线刷img镜像,无奈 网上下了个可以线刷的工具套件 流氓ROM . 使用HuaweiUpdateExtractor(工具百度)把官方 ...
- Add project to working sets
最近换了个电脑,重新搭建了开发环境,但是在新建项目的过程中发现有Add project to working sets这一个选项,一开始也不明白是什么意思,百度了一下,不少网友说是把项目存到物理空间, ...
- Java内存模型---并发编程网 - ifeve.com
Java内存模型 转自:http://ifeve.com/java-memory-model-6/ 原文地址 作者:Jakob Jenkov 译者:张坤 Java内存模型规范了Java虚拟机与计算机 ...
- ALAssets的两种用法
一: ALAssetsGroupEnumerationResultsBlock resultsBlock = ^(ALAsset *result, NSUInteger index, BOOL *st ...
- android onclick onLongClick ontouch dispatchTouchEvent onInterceptTouchEvent
android onclick onLongClick ontouch dispatchTouchEvent onInterceptTouchEvent 按ACTION_DOWN -> onLo ...
- 生成Apk遇到的问题
conversion to dalvik format failed with error 1 android proguard keep Parameterized class
- Java容器类概述
1.简介 容器是一种在一个单元里处理一组复杂元素的对象.使用集合框架理论上能够减少编程工作量,提高程序的速度和质量,毕竟类库帮我们实现的集合在一定程度上时最优的.在Java中通过java.util为用 ...
- N的阶乘的长度 V2(斯特林近似) 求 某个大数的阶乘的位数 .
求某个大数的阶乘的位数 . 得到的值 需要 +1 得到真正的位数 斯特林公式在理论和应用上都具有重要的价值,对于概率论的发展也有着重大的意义.在数学分析中,大多都是利用Г函数.级数和含参变量的积分等 ...
- PHP中的错误处理和异常处理
错误处理: 1.语法错误 2.运行时的错误 3.逻辑错误 错误报告: 错误E_ERROR 警告E_WARNING 注意E_NOTICE 开发 ...