/*
* 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. How To:Linux下如何通过命令检查网卡是否插上网线

    主要工具为ethtool来检查,主要关注的字段为"Link detected",注意如下的输出,其中em4实际物理上并未插上网线,而em1是插上网线的: # ethtool em4 ...

  2. (16)zabbix history trends历史与趋势数据详解

    1. 保留历史数据 我们可以通过如下方式来设置保留数据的时长:监控项(item)配置里匹配更新监控项(item)设置Housekeeper tasksHousekeeper会定期删除过期的数据.如果数 ...

  3. perl学习之内置变量

    Perl内置特殊变量   一.正则表达式特殊变量:1.$n  :包含上次模式匹配的第n个子串2.$& :前一次成功模式匹配的字符串3.$`  :前次匹配成功的子串之前的内容4.$’ :前次匹配 ...

  4. web开发框架之DRF

    RESTful架构解释: # Representational State Transfer 表现层状态转化 到底什么是RESTFul架构: 如果一个架构符合REST原则,就称它为RESTful架构. ...

  5. Java-重定义标准输出和错误

    刚接触到错误日志的时候觉得很神奇,今天学习了一番后用代码写了出来. package com.tj; import java.io.File; import java.io.FileNotFoundEx ...

  6. E. A Magic Lamp

    E. A Magic Lamp Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB   64-bit integer IO ...

  7. TOJ 4815: 关押罪犯

    4815: 关押罪犯 Time Limit(Common/Java):10004MS/12000MS     Memory Limit:65536KByte Total Submit: 2       ...

  8. 牛腩新闻发布系统(二):SQLHelper重构(二)

    导读:在上一篇博客中,介绍了简单的SQLHelper重构,即数据库链接,打开和关闭链接的优化等.现在,将介绍参数化查询和执行命令类型的改造. 一.必要性 1,参数化查询 在上篇博客中,在查询的时候,仅 ...

  9. Datatable 生成json格式

    public string GetJsonFromDataTable(DataTable dt, int total, bool ShowFooter, string fields, string i ...

  10. Distimo发布新SDK 帮助开发者跟踪应用下载转换率

    著名应用分析机构Distimo近日刚刚发布了新的SDK,可以帮助开发者更好地跟踪应用下载转换率,同时也可以帮助开发者更好地了解多个在线营销活动的表现. Distimo发布的新工具叫做“Distimo ...