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,该列作废.这样 ...
随机推荐
- Python IDLE 运行错误:IDLE's subprocess didn't make connection. --已解决(原创)!
Python IDLE 错误描述: Subprocess Startup ErrorIDLE's subprocess didn't make connection. Either IDLE can' ...
- PHP - 递归函数
/** * factorial($num) 计算阶乘 * @param string $num * @return string $total */ function factorial($num) ...
- PHP - 概述
第1章 PHP概述 学习要点: 1.PHP基础知识 2.PHP的环境配置 3.安装三款主流程序 4.PHP开发工具的选择 5.一个简单的示例 一.PHP基础知识 PHP PHP是一种目前最流行的服务端 ...
- wamp安装后打开默认网页显示dir,图标红点
首先网页显示dir,是因为服务这些没启动,跟图标红点是一个原因,解决了图标红点,就能解决只显示dir的问题. 先测试是不是端口占用问题,如图: 如果是,那就继续往下走. 单击图标左键(记住是左键),然 ...
- ORA-01092 ORA-12432: LBAC error: zllegnp:OCIStmtExecute 故障一例
最近由于数据库hang住,无奈之下直接干掉了pmon进程,再次启动的时候收到了ORA-01092: ORACLE instance terminated. Disconnection forced以及 ...
- 第二代map-reduce架构YARN解析
需求 我们在考虑hadoop map-reduce框架的时候,最重要需包括: 1. reliability 可靠性,主要是jobtracker,resource manager可靠性 2. avail ...
- [置顶] Windows Phone后台音乐详解一
应用于: Windows Phone 8 | Windows PhoneOS 7.1 你可以为winphone编写在后台播放音乐的app.这表示即使当用户点击返回或开始按钮离开你的应用界面时,你的应用 ...
- 在Ubuntu下编译Assimp库
在Ubuntu下编译Assimp库 怎样在Ubuntu下编译Assimp库?这是我曾经编译成功后做的笔记,供參考. 1.去以下的站点去下载Assimp库: http://assimp.sourcefo ...
- Linux编译多个不同目录下的文件以及静态库、动态库的使用
先看两篇博文,作为基础知识.如果对C/C++编译链接过程都了解的话,可以跳过不看. http://www.firedragonpzy.com.cn/index.php/archives/2556 ht ...
- 屏蔽EditText长按导致的弹出输入法的对话框
做了个能手动拖动的EditText,但有个问题导致的体验很不好,就是手放上去开始拖,拖到一段距离后弹出个输入法的对话框,这根本不是我想要的效果,于是就想屏蔽它,结果在网上找到一句代码,放上去 顿时解决 ...