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行数组的对应表.所以对于二分查找法的初始值为: ...
随机推荐
- how to get many stars on Github?
some key points: 1: make a beautiful README file2: use some GIF (google some tools to convert videos ...
- element-ui date-picker 设置结束时间大于等于开始时间且开始时间小于等于结束时间
Part.1 问题 date-picker 组件在使用时,默认对时间是没有限制的,可以随便选择区间,官方文档添加了快捷选项,如:一周丶一月... 但是从用户体验方面出发,我们还是希望对时间进行有利的 ...
- CNN完成mnist数据集手写数字识别
# coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data d ...
- GetForgroundWindow函数的不确定性——BUG笔记
HWND GetForgoundWindows() 获取当前前置窗口在windows 7和windows 10下虚拟桌面切换后表现不同. 所以强烈不建议使用此函数!
- 16.04 下修改 ssh 默认端口
打开/etc/ssh/ssh_config,在Port指令下追加新的端口设置: Port 8888 即允许通过端口 8888 进行 ssh 访问. 打开/etc/ssh/sshd_config,进行同 ...
- C++ Simple Message/Logging Class
在 Qt的源码与Protobuf 的代码中,看到相同的简单消息(日志)输出的类实现,基本思路是使用宏定义,重载临时类对象,调用类方法或者通过析构函数自动调用输出方法,实现消息输出.这里以 Protob ...
- UVa-401-Palindromes(回文)
这一题的话我们可以把映像字符的内容给放入一个字符串常量里面,然后开辟一个二维的字符串常量数组,里面放置答案. 对于回文实际上是很好求的,对于镜像的话,我们写一个返回char的函数,让它接收一个char ...
- Linux基础测试
目 录 第1章 文件及目录课后作业 1 第2章 Linux打包与压缩习题 1 第3章 Linux系统VIM编辑器习题 1 文件及目录课后作业 从/proc/meminfo中过滤出 ...
- 【集合遍历-Java】
遍历List集合的三种方法 1.增强for循环 for(String str : list) {//其内部实质上还是调用了迭代器遍历方式,这种循环方式还有其他限制,不建议使用. System.out. ...
- webpack 之 loader
loader简介 loader在webpack.config.js中进行配置,配置之后,会自动检测打包过程中引入的文件(import或require),通过test成功匹配被引入的文件名后,会对文件内 ...