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 ...
随机推荐
- hashmap and hashtable
①继承不同. public class Hashtable extends Dictionary implements Map public class HashMap extends Abstrac ...
- xlistview的(java)
package com.bwie.xlistviews; import java.text.SimpleDateFormat;import java.util.Date; import com.bwi ...
- [windows驱动]windows8.1驱动调试前戏
人们都说在干正事之前,得先做足前戏才会爽,我一直很认同这个观点,下面我来总结下进行windows8.1的WDK调试所要做的准备工作. 软件安装: 1.VS2013. 2.WDK8.1 3.Window ...
- STM32下载显示target dll has been cancelled
使用MDK 4.74向STM32下载时出现各种错误,而且时隐时现, Internal command error.Error:Flash download failed. Target DLL has ...
- C++ inline weak symbol and so on
关于inline这个关键字,听到强调得最多的是,它只是一种对于编译器的建议,而非强制执行的限定. 但事实上,即使这个优化最终由于函数太过复杂的原因没有达成,加上inline关键字(还有在类定义中直接定 ...
- Osmocom-BB中cell_log的多种使用姿势
转载留做备份,原文地址:http://92ez.com/?action=show&id=23342 翻阅osmocom-bb源码的时候注意到,在cell_log中有非常多我们从来没有注意到的功 ...
- 使用iskindofclass来发现对象是否是某类或其子类的实例
发现对象是否是特定类或其子类的实例 要发现对象是否是某类或其子类的实例,请在对象上调用 isKindOfClass: 方法.当应用程序需要发现其响应的消息(实现的或继承的),它有时进行以上的检查. s ...
- 51 nod 机器人走方格
从一个长方形的方格的右上角 走到 左下角 , 问一共有多少种不同的路线可以达到 . #include<stdio.h> #include<string.h> #include& ...
- HDU 4349 Xiao Ming's Hope
有这样一个性质:C(n,m)%p=C(p1,q1)*C(p2,q2).......%p,其中pkpk-1...p1,qkqk-1...q1分别是n,m在p进制下的组成. 就完了. #include&l ...
- office2010安装Microsoft Office Document Imaging (MODI) 图解
office2010安装Microsoft Office Document Imaging (MODI) 图解 Microsoft Office 2010 中删除了 Microsoft Off ...