Java for LeetCode 074 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.
解题思路:
二分查找即可,JAVA实现如下:
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0)
return false;
int up = 0, down = matrix.length-1, left = 0, right = matrix[0].length-1;
while (up < down) {
if(target>matrix[(up+down)/2][0]){
up=(up+down)/2;
if(target>matrix[up][matrix[0].length-1])
up++;
else break;
}
else if(target<matrix[(up+down)/2][0])
down=(up+down)/2-1;
else return true;
}
while(left<right){
if(target>matrix[up][(left+right)/2])
left=(left+right)/2+1;
else if(target<matrix[up][(left+right)/2])
right=(left+right)/2-1;
else return true;
}
return target==matrix[up][left];
}
当然,也可以只用两个指针表示,JAVA实现如下:
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0)
return false;
int l = 0, r = matrix.length * matrix[0].length - 1, mid = 0;
while (l < r) {
mid = l + (r - l) / 2;
if (matrix[mid/matrix[0].length][mid%matrix[0].length] < target)
l = mid + 1;
else if (matrix[mid/matrix[0].length][mid%matrix[0].length] > target)
r = mid;
else return true;
}
return matrix[l/matrix[0].length][l%matrix[0].length] == target;
}
Java for LeetCode 074 Search a 2D Matrix的更多相关文章
- Leetcode 074 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] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
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(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- [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
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 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(搜索二维矩阵)
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, ...
随机推荐
- opencv笔记2:图像ROI
time:2015年 10月 03日 星期六 12:03:45 CST # opencv笔记2:图像ROI ROI ROI意思是Region Of Interests,感兴趣区域,是一个图中的一个子区 ...
- PL/0编译器(java version) - Err.java
1: package compiler; 2: 3: import java.io.BufferedWriter; 4: 5: public class Err { 6: 7: publi ...
- eclipse中文乱码问题解决方案
eclipse之所以会出现乱码问题是因为eclipse编辑器选择的编码规则是可变的.一般默认都是UTF-8或者GBK,当从外部导入的一个工程时,如果该工程的编码方式与eclipse中设置的编码方式不同 ...
- java 打包jar文件以在没有安装JDK或JRE的机子上运行
前言: java号称“一次编译,到处运行”,但这有个前提,那就是你的机子上得安装java环境.对于开发人员或其他一些比较懂计算机的人来说这没什么,但是对于一些不懂计算机的人来说这会很麻烦,他们更希望的 ...
- php 数组二分法查找函数
找到返回对应的key,找不到返回-1,注意二分查找需要数组有序,下边函数需要数组递增排序. function binarySearch($arr,$x){ $start=0; $end=count($ ...
- pom.xml
使用intelJ idea 导入maven包管理文件是,使用Import的方式导入,会自动导入pom.xml来导入包. pom.xml会指定父子关系. 例如,总模块的pom.xml中有一下内容: &l ...
- user32.dll
user32.dll中的所有函数 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- WPF 检测计算机网络连接情况
; ; ; ; [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(ou ...
- struts2 访问国际化资源 <s:text>作为属性
保留全局级国际化信息资源文件.并在message.properteis中增加一个带有参数的国际化信息. labela = labela in zh_CN labelb = labelb,{0} < ...
- ldconfig deferred processing now taking place
在ubuntu下面安装软件,安装结束后,提示:ldconfig deferred processing now taking place 到网上查询了一下,大概意思是说:软件安装完了,是否要重启电脑.