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行数组的对应表.所以对于二分查找法的初始值为: ...
随机推荐
- DBMS数据库语言
https://www.yiibai.com/dbms/dbms-language.html DBMS具有适当的语言和接口来表达数据库查询和更新.数据库语言可用于读取,存储和更新数据库中的数据. 数据 ...
- Unity3D windows平台视频录制录屏插件 UnityRecorder
例子:从官方例子简单改了 using UnityEditor;using UnityEditor.Recorder;using UnityEditor.Recorder.Input;using Sys ...
- Python 访问字典(dictionary)中元素
访问python字典中元素的几种方式 一:通过“键值对”(key-value)访问: print(dict[key]) dict = {1: 1, 2: 'aa', 'D': 'ee', 'Ty': ...
- Mathematics-基础:斐波那契数列
f(1)=1 f(2)=1 f(n)=f(n-1)+f(n-2) n>2
- GC相关的面试题
问题:Object的finaliz()方法 的作用是否与C++的析构函数作用相同? --->不同的 1.C++的析构函数调用确定,就是对象离开作用域之后就马上被删除.而java Object的f ...
- onPullDownRefresh函数没有被正确执行
原因 问题原因很多,我遇到的这个问题的原因是: 页面有两个同名的onPullDownRefresh函数,导致只执行最后的一个. 解决 只留一个onPullDownRefresh函数
- POJ-1163 递推
代码很容易看明白,就不详解了. 这个是空间优化的代码. #include <iostream> #include <algorithm> #define MAX 101 usi ...
- CSS3---渲染属性
1.计数器 CSS3计数器( CSS Counters )可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能.与有序列表相比,它的突出特性在于可以对任意元素计数,同时实现个性化 ...
- Linux使用yum命令安装软件时,连接不了网路报错:ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
错误: 解决方案: 在/etc/sysconfig/network-scripts/ifcfg-eth0文件中配置DNS信息: vim /etc/sysconfig/network-scripts/i ...
- 【转】windows下nginx+mono+fastCGI部署asp.net网站
原文链接:http://www.cnblogs.com/amityat/archive/2011/08/23/2150153.html 1,什么是nginx 简介Nginx ("engine ...