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

  1. 240.Search in a 2D Matrix II

    /* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...

  2. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

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

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

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

  5. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

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

  7. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

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

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

随机推荐

  1. shell脚本,100以内的质数有哪些?

    [root@localhost wyb]# cat 9zhishu.sh #!/bin/bash ` do ;j<=i-;j++)) do [ $((i%j)) -eq ] && ...

  2. CentOS 7 升级gcc/g++编译器

    gcc的升级必须要使用源码进行升级,也就说,必须要使用源码进行编译才行.我的7.2的CentOS目前自带的gcc是4.8.5的,gcc从4.8之后开始支持C++11,但是鉴于现在C++14.C++17 ...

  3. mysql:having 用法

    顺序:where -> group by -> min -> order by -> limit 在select语句中使用having 子句来指定一组行或聚合的过滤条件 hav ...

  4. Python Third Day-函数

    ''' 为什么要有函数?没有函数带来的困扰? 组织结构不清晰,可读性差 代码冗余 可扩展性差 什么是函数 具备某一个功能的工具--->函数 事先准备工具->函数的定义 拿来就用.重复使用- ...

  5. vuex其实超简单,只需3步

    前言 之前几个项目中,都多多少少碰到一些组件之间需要通信的地方,而因为种种原因,event bus 的成本反而比vuex还高, 所以技术选型上选用了 vuex, 但是不知道为什么,团队里的一些新人一听 ...

  6. 对于js运动中产生的问题

    1.不同的对象调用同一个定时器情况,则需要将定时器的名称定为该对象的一个属性来进行运用. 例: <!DOCTYPE html> <html lang="en"&g ...

  7. Python9-反射-day27(大年初三)

    复习 class 类名(父类,父类2): 静态属性 = '' #静态属性 类属性 def __init__(self): #初始化方法 self.name = 'alex' def func(self ...

  8. (转) [C++]我再也不想在任何头文件中看到using namespace xxx这种句子了(译)

    原文的传送:I don’t want to see another “using namespace xxx;” in a header file ever again 转自  http://blog ...

  9. 1,python初识

    什么是变量? 变量:将程序的中间结果暂时存储起来,以便后续程序调用. 什么是字符串类型? python中被引号引起来的数据就是字符串.字符串类型,也简称str类型. 在python中 int是什么? ...

  10. lfyzoj104 Counting Swaps

    问题描述 给定你一个 \(1 \sim n\) 的排列 \(\{p_i\}\),可进行若干次操作,每次选择两个整数 \(x,y\),交换 \(p_x,p_y\). 请你告诉穰子,用最少的操作次数将给定 ...