74.Search in a 2D Matrix
/*
* 74.Search in a 2D Matrix
* 12.5 by Mingyang
* 这里面的对应挺巧的:
* 这个就是将2D矩阵转化成1行数组的对应表。所以对于二分查找法的初始值为:
* low=0,high=rows*columns-1(总共数值的个数,因为从0开始所以减1)。
* 为了能够方便在given 2D matrix找到需要比对的值
* 我们还是需要确定行数和列数,通过上表可以看出,行数是position/columns,而列数是position%columns,
*/
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0 || matrix == null)
return false;
int rows = matrix.length;
int cols = matrix[0].length;
int low = 0;
int high = rows * cols - 1;
while (low <= high) {//一定要等号,因为可能存在low,high相等的情况下,还等于target
int mid = (low + high) / 2;
int midValue = matrix[mid / cols][mid % cols];
if (midValue == target)
return true;
else if (midValue < target)
low = mid + 1;
else
high = mid - 1;
}
return false;
}
74.Search in a 2D Matrix的更多相关文章
- 240.Search in a 2D Matrix II
/* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [LeetCode] 74. 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 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- 74. Search a 2D Matrix
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- Leetcode 74 and 240. Search a 2D matrix I and 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 ...
随机推荐
- C++后台知识点总结(一)
C++基础部分: 1.数组和指针的区别 (1)数组本身体现出来的就是一个 指针常量的 “特性”,即不能对数组的首地址进行修改,内存上的地址就已经是确定了的.而指针本身是一个变量,他指向了一个地址,这个 ...
- LeetCode 最大正方形
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4解法:判 ...
- MySQL丨01丨数据库基本概念
以前记录数据可能很少也很简单,比如说老王借了老李半斤肉,这样的数据老李直接就写到墙上就行了. 后来数据多了人们就以表格的方式开始记录,写到一张A4纸上,比如学生的档案,有表头和序号等. 表头里有姓名. ...
- python多进程与多线程编程
进程(process)和线程(thread)是非常抽象的概念.多线程与多进程编程对于代码的并发执行,提升代码运行效率和缩短运行时间至关重要.下面介绍一下python的multiprocess和thre ...
- 格式化输出,基本运算符,流程控制主if
5.5自我总结 一.格式化输出 1.占位符 a = 1 b = 2 print('%S %s'%(a,b)) #1 2 print('%s %s'%(1,2)) #1 2 2.format格式化 a ...
- c++ 快速幂 代码实现
懒得打代码系列… 不过这个代码挺短的死背下来也ok 解析在最下面 建议自己手动试个数据理解一下 比如 3^5 ^^ 原理:a ^ b = a ^ (b / 2) * 2 (b是奇数的话还要再乘一个a) ...
- 爬虫开发python工具包介绍 (4)
本文来自网易云社区 作者:王涛 此处我们给出几个常用的代码例子,包括get,post(json,表单),带证书访问:Get 请求 @gen.coroutine def fetch_url(): ...
- 第五部分 linux系统管理员 开机流程 模组管理 与loader
第五部分 linux系统管理员 开机流程 模组管理 与loader 开机流程分析 cmos保存电脑硬件的参数 bios 基本的输入输出系统 读取硬件的软件 MBR master bo ...
- Laya Timer原理 & 源码解析
Laya Timer原理 & 源码解析 @author ixenos 2019-03-18 16:26:38 一.原理 1.将所有Handler注册到池中 1.普通Handler在handle ...
- hdu2063 二分图匹配,匈牙利算法
#include <stdio.h> #include <string.h> int n1,n2,m,ans; ]; //记录V2中的点匹配的点的编号 ]; //记录V2中的每 ...