LeetCode——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.
原题链接:https://oj.leetcode.com/problems/search-a-2d-matrix/
题目:在m * n 的矩阵中查找目标值是否存在。
比較好的办法就是二分查找。即将矩阵总体看成一个数组来处理。
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
return false;
int m = matrix.length;
int n = matrix[0].length;
int start = 0;
int end = m * n - 1;
while (start <= end) {
int mid = (start + end) / 2;
int x = mid / n;
int y = mid % n;
if (matrix[x][y] == target)
return true;
if (matrix[x][y] < target)
start = mid + 1;
else
end = mid - 1;
}
return false;
}
LeetCode——Search a 2D Matrix的更多相关文章
- [LeetCode] Search a 2D Matrix 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 ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LeetCode: Search a 2D Matrix 解题报告
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 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 ...
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [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 ...
- [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 ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
随机推荐
- Java8 Lamdba表达式 001
在一个已经存在的编程语言里非常少有对现有的生态系统起重大影响的新特性.Lambda表达式对于Java语言就是这样的意义的存在.简单来说,Lambda表达式提供了便利的方式去创建一个匿名的功能.提供了一 ...
- c语言,enum
在设置错误代号时,使用enum比宏更好看. #include <stdio.h> typedef enum { retOK = 0, errComInterruption = 0x1000 ...
- Emmet插件
p{font-size: 18px; color: #666;} body{background-color:#F3F3F3} .code{color:#3974C3;font-size: 14px; ...
- ASC(22)H(大数+推公式)
High Speed Trains Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Su ...
- MSSQL - 因为数据库正在使用,所以无法获得对数据库的独占访问权。
关于“因为数据库正在使用,所以无法获得对数据库的独占访问权”的最终解决方案 今天在使用SQL Server2005对某个数据库进行还原操作的时候,出现了如上问题,“因为数据库正在使用,所以无法获得 ...
- WCF随笔3----消息编码器
原文:WCF随笔3----消息编码器 我们都知道,message是wcf通信框架进行通信的最基本的单位,但是wcf开发人员其实根本不需要直接与message打交道,一样能够写好wcf相关的程序.这是因 ...
- webdynpro MESSGAE
1. 添加辅助类CL_WDR_DEMO_MESSAGES 环境,设计的控件有:输入控件,按钮,每个按钮对应一个事件.分别是下面,然后报消息 TEXT: SUCCESS: method ONACTIO ...
- Tomcat详细用法学习(二)
本篇接上一篇<Tomcat详细用法学习(一)>,主要讲解服务器的虚拟目录映射的几种方式. 先来看几个概念: web应用的概念:一个web应用包含了许多我们做好的web资源,里面或许包括了多 ...
- 超炫的Button按钮展开弧形动画效果
----------------------收藏备用 ------------------------------- 代码下载:http://download.csdn.net/detail/qq2 ...
- 几款屏幕录制软件 ActivePresente
几款屏幕录制软件,最强大是 ActivePresenter ,免费版, 足以应对我们日常需求.列表如下 支持系统:W-Windows,L-Linux,M-Mac 软件 格式 W L M 免费 说明 ...