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,该列作废.这样 ...
随机推荐
- hdu 5015 大数量反复类似操作问题/ 矩阵高速幂
题意: 给一个矩阵a,第一行是 0. 233,2333,23333.....第一列读入.列数<10^9.行数<=10. 先转化操作: m是大数量.必定每次向前推一列.就是每次乘一个矩阵T. ...
- bin文件格式分析
xip 的 bin 文件分析 一个bin 文件在存储上是按以下的结构存储的 组成:标记(7)+Image開始地址(1)+Image长度(1) 记录0地址+记录0长+记录0校验和+ ...
- vtk基础编程(2)-读取数据文件中的坐标点
原文地址: http://blog.csdn.net/chinamming/article/details/16860051 1. 案例说明 在实际计算中,常常需要大量的数据, 这个时候数据文件就必不 ...
- android 设置字体颜色、EditText自己主动输入转换成大写字母的多种方式
在TextView上面设置某一个字的字体颜色为指定颜色时,能够通过java类SpannableString类和Html语言来实现. (一)SpannableString类方式 private void ...
- 【Cocos2d-X开发学习笔记】第28期:游戏中音乐和音效的使用
本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 UI在游戏中占有很重要的地位,但吸引玩家的除了这 ...
- Cocos2d-x学习笔记(5)
Cocos2d-x基本元素介绍: 1.CCDirector:大总管 这是控制游戏流程的主要组件,包含设定游戏呈现窗体.FPS显示.默认帧率上限.纹理颜色位宽等切换当前游戏场景.暂停或恢复场景执行.通过 ...
- 【app】遍历目录所有文件
遍历目录所有文件 原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:htt ...
- 编译android-4.3.1_r源代码并刷到自己的Galaxy Nexus I9250真机上
编译android-4.3.1_r源代码并刷到自己的Galaxy Nexus I9250真机上 作者:雨水 日期:2014-04-30 编译源码的目的还是为了自己改动源码,然后还可以执行在相应的手机 ...
- 自适应滤波器(Adaptive Filter)
======= Wikipedia的解释 ======= 自适应滤波器是能够根据输入信号自动调整性能进行数字信号处理的数字滤波器.作为对比,非自适应滤波器有静态的滤波器系数,这些静态系数一起组成传递函 ...
- 14.6.1 Creating InnoDB Tables 创建InnoDB 表:
14.6.1 Creating InnoDB Tables 创建InnoDB 表: 创建一个InnoDB 表,使用CREATE TABLE 语句,你不需要指定 ENGINE=InnoDB子句 如果In ...