240.Search in a 2D Matrix II
/*
* 240.Search in a 2D Matrix II
* 2016-6-17by Mingyang
* From left-bottom to right-top
* 他这道题目虽说是用了BS,但是并不一定要有mid,这里就从最左下角到右上角
* 这个数大于同一列的,小于同一行的,只要跟target比较以后,就可以要么删除一列,要么删除一行--------找拐点
* 这个题目自己做的时候给出了一个nlog的方案,先是row从下往上走,在同一row里面再继续bs
* 但是这个题目的好方法就是从左下角出发,要么往右走,要么往上走
* 如果比目标大,表示我这一行都比目标大,那么我就往上走一个
* 如果比目标小,这一列都比目标小,那么往右走一个
*/
public boolean searchMatrix2(int[][] matrix, int target) {
if (null == matrix || matrix.length == 0) {
return false;
}
int row = matrix.length - 1;
int col = 0;
while (row >= 0 && col < matrix[0].length) {
if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] < target) {
col++;
} else {
row--;
}
}
return false;
}
/*
* 用了DC思想,就是根据与中间的大小,来舍弃一个部分的值
* Divide-Conquer,Based on the mid of the matrix, divide the whole matrix into four parts: left-top, left-bottom, right-top, right-bottom
* If the mid is smaller than target, abandon the left-top area, recursion from the other three areas.
* If the mid is larger than target, abandon the right-bottom area, recursion from the other three ares.
* Time Complexity formula:T(n) = 3T(n/2) + c
*/
public boolean searchMatrixImprove(int[][] matrix, int target) {
if (null == matrix || matrix.length == 0) {
return false;
}
return helper(matrix, 0, matrix.length - 1, 0, matrix[0].length - 1, target);
}
private boolean helper(int[][] matrix, int rowStart, int rowEnd, int colStart, int colEnd, int target) {
if (rowStart > rowEnd || colStart > colEnd) {
return false;
}
int rowMid = rowStart + (rowEnd - rowStart) / 2;
int colMid = colStart + (colEnd - colStart) / 2;
if (matrix[rowMid][colMid] == target) {
return true;
} else if (matrix[rowMid][colMid] > target) {
return helper(matrix, rowStart, rowMid - 1, colStart, colMid - 1, target) ||
helper(matrix, rowMid, rowEnd, colStart, colMid - 1, target) ||
helper(matrix, rowStart, rowMid - 1, colMid, colEnd, target);
} else {
return helper(matrix, rowMid + 1, rowEnd, colMid + 1, colEnd, target) ||
helper(matrix, rowMid + 1, rowEnd, colStart, colMid, target) ||
helper(matrix, rowStart, rowMid, colMid + 1, colEnd, target);
}
}
240.Search in a 2D Matrix II的更多相关文章
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- 【刷题-LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- LintCode 38. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...
- Search a 2D Matrix | & II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...
- 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 ...
- 74.Search in a 2D Matrix
/* * 74.Search in a 2D Matrix * 12.5 by Mingyang * 这里面的对应挺巧的: * 这个就是将2D矩阵转化成1行数组的对应表.所以对于二分查找法的初始值为: ...
随机推荐
- vue下axios和fetch跨域请求
1.在config的index.js下面进行常用跨域配置代码:proxyTable: { '/apis': { //使用"/api"来代替"http://xxxx.cn& ...
- jmeter的JVM参数设置
JMeter用户可根据运行的计算机配置,来适当调整JMeter.bat中的JVM调优设置,如下所示: set HEAP=-Xms512m -Xmx512m set NEW=-XX:NewSize=12 ...
- 8.2.6 PEB —— PEB结构值不正确的问题
书中作者使用 dt _PEB xxxxxx 命令来查看当前进程的PEB结构. 实际操作后PEB结构显示的成员值: 作为进程链表的LDR结构居然没有值,这显然是不正常的,地址也没有输错,问题到底出在哪里 ...
- maven打包的含义
我们在用maven构建java项目时,最常用的打包命令有mvn package.mvn install.deploy,这三个命令都可完成打jar包或war(当然也可以是其它形式的包)的功能,但这三个命 ...
- VBA Promming——入门教程
VBA Visual Basic for Applications(VBA)是Visual Basic的一种宏语言,是微软开发出来在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言.主要能用来 ...
- c++ vector容器遍历方式
#include <vector> #include <iostream> class Test { public: int a; int b; int c; Test() { ...
- Mathematics-基础:斐波那契数列
f(1)=1 f(2)=1 f(n)=f(n-1)+f(n-2) n>2
- JavaEE-04 数据源配置
学习要点 JNDI 数据库连接池 完成新闻发布系统数据库连接池 JNDI 说明 JNDI(Java Naming and Directory Interface),中文翻译为Java命名与目录接口,是 ...
- BZOJ3940: [Usaco2015 Feb]Censoring (AC自动机)
题意:在文本串上删除一些字符串 每次优先删除从左边开始第一个满足的 删除后剩下的串连在一起重复删除步骤 直到不能删 题解:建fail 用栈存当前放进了那些字符 如果可以删 fail指针跳到前面去 好菜 ...
- 什么是session?
Session一般译作会话.从不同的层面看待session,它有着类似但不全然相同的含义.比如,在web应用的用户看来,他打开浏览器访问一个电子商务网站,登录.并完成购物直到关闭浏览器,这是一个会话. ...