/*
* 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的更多相关文章

  1. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  2. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  3. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  4. 【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 ...

  5. 【刷题-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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 74.Search in a 2D Matrix

    /* * 74.Search in a 2D Matrix * 12.5 by Mingyang * 这里面的对应挺巧的: * 这个就是将2D矩阵转化成1行数组的对应表.所以对于二分查找法的初始值为: ...

随机推荐

  1. Netbeans Makefile: recipe for target 'XXX' failed 运行failed(退出值 -1073741511 找不到C/C++库文件,关键字

      今天不知怎么的又出错了 吐血了 找不到NULL new等关键字了   看到知乎上有人和我一个问题,怎么办?很简单卸载了以前的cygwin和netbeans然后重装,我重装时没有把以前的cygwin ...

  2. JavaFX Chart设置数值显示

    一.XYChart import javafx.application.Application;import javafx.geometry.NodeOrientation;import javafx ...

  3. Python3简明教程(九)—— 文件处理

    文件是保存在计算机存储设备上的一些信息或数据.你已经知道了一些不同的文件类型,比如你的音乐文件,视频文件,文本文件.Linux 有一个思想是“一切皆文件”,这在实验最后的 lscpu 的实现中得到了体 ...

  4. 网新恩普(T 面试)

    1.你们项目中哪里用到了Redis? 2.介绍一下你的这个微服务项目

  5. String系列之replaceAll方法替换.

    直接使用String类的replaceall方法的第一个参数并不是简单的字符串,而是一个正则表达式.在正则表达式中,英文点号(.)表示任意字符,所以你原先的写法会把所有字符都替换成空白. 转义使用所以 ...

  6. autoOpenBrowser: true, 运行npm后自动打开浏览器

    autoOpenBrowser: true, 运行npm后自动打开浏览器

  7. #PHP#微信支付 第二篇 JSAPI 调用统一下单接口获取预支付交易数据

    上一篇讲到成功获取 openid,本篇要调用微信统一接口创建预支付交易单,并获取到相关数据,以便(后边)在微信内调起H5支付 第三步,调用微信统一下单接口创建预支付交易单 微信统一下单API是微信支付 ...

  8. nodeJS和npm的环境配置

    1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL” ...

  9. vs2008控件查看器

    使用 OLE/COM 对象查看器 通过读取控件的类型库,OLE/COM 对象查看器使您得以查看控件的接口. 使用 OLE/COM 对象查看器 单击“工具”菜单上的“OLE/COM 对象查看器”或在命令 ...

  10. 苹果平台上的媒体流播放技术HLS

    近日在和朋友聊起媒体流的服务器端实时转码技术的时候,发现苹果的各种终端上的视频播放并未使用常见的基于UDP的RTSP/RTP,而强制使用了Http Live Stream技术,这里稍稍总结了如下. 苹 ...