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 following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
题目标签:Array
题目给了我们一个有序的矩阵,让我们找target是否在矩阵中。首先我们分析,如果我们知道了target 在哪一行的话,直接用binary search 就可以了。所以第一步可以用binary search 来搜索第一列,确认target在哪一行。在用一次二分法对于那一行,找出target。
Java Solution:
Runtime beats 71.85%
完成日期:07/23/2017
关键词:Array
关键点:用二分法两次
public class Solution
{
public boolean searchMatrix(int[][] matrix, int target)
{
if(matrix.length == 0 || matrix[0].length == 0)
return false;
if(target < matrix[0][0] || target > matrix[matrix.length-1][matrix[0].length-1])
return false;
// first determine which row the target is in
int top = 0;
int bottom = matrix.length-1; while(top <= bottom)
{
int mid = top + (bottom - top) / 2; if(matrix[mid][0] == target)
return true;
else if(matrix[mid][0] > target)
bottom = mid - 1;
else
top = mid + 1;
} int rowNum = bottom; // use binary search for this row
int left = 0;
int right = matrix[0].length-1; while(left <= right)
{
int mid = left + (right - left) / 2; if(matrix[rowNum][mid] == target)
return true;
else if(matrix[rowNum][mid] < target)
left = mid + 1;
else
right = mid - 1; } return false;
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/4323301.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 74. Search a 2D Matrix(搜索二维矩阵)的更多相关文章
- 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] search a 2d matrix 搜索二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 074 Search a 2D Matrix 搜索二维矩阵
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[ [1, 3, ...
- Leetcode74. Search a 2D Matrix搜索二维矩阵
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...
- [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 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [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
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- search a 2D matrix(在二维数组中搜索一个元素)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- PowerDesigner连接MySQL和逆向工程图
0.写在前面的话 最近想梳理公司项目的表间关系,从项目后台管理系统的操作入手,以及代码的hibernate注解入手,都不算特别尽人意,于是最后还是鼓捣了一下PowerDesigner的逆向工程图,这样 ...
- Java通过链表实现栈
class LinkedStack<T> { private Node top; private int size; /** * 初始化栈 */ public LinkedStack() ...
- UDP接收百万级数据的解决方案
小序 到新公司不久,就接到一个任务:有个发送方,会通过udp发送一些信息,然后服务接收到信息后保存到数据库的一张表A,保存的这些数据在经过一系列处理,处理完成后累积到另一张表B,然后清空处理的表A的数 ...
- 机器视觉----LBP
最近一直在做多视图的聚类与分裂,想要图片有更多的视图,就得对图片的特征进行抽取,那我们来聊聊图片的LBP特征. Local binary patterns (局部二值模式),是机器视觉中重要的一种特征 ...
- CSV导出大量数据
$csvname = $csvname . '.csv'; header('Content-Type: application/vnd.ms-excel;charset=GB2312'); heade ...
- [js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解
路径在canvas绘图中,经常被用到,是一个非常重要的概念. 比如:我们要在canvas画出3条直线,要求用不同的颜色加以区分. <style> body { background: #0 ...
- BZOJ-1012-[JSOI2008]最大数maxnumber(线段树)
Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插 ...
- mysql技能提升篇 - Sqlyog高级应用
mysql作为绝大部分公司使用的数据库,自然是牛牛牛! 每个人都能设计数据库,都能从删库到跑路.但是,如何做到更好,更快,更准地建立你的mysql数据库,这是个值得关注的问题(尽管很多人已经去搞大数据 ...
- Quartz源码——JobStore保存JonDetail和Trigger源码分析(一)
我都是分析的jobStore 方式为jdbc的SimpleTrigger!RAM的方式类似分析方式! {0} :表的前缀 ,如表qrtz_trigger ,{0}== qrtz_ {1}:quartz ...
- 简单说明CGI是什么
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...