LeetCode——Search a 2D Matrix II
Description:
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 in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
首先想到的就是遍历整个矩阵,时间复杂度是O(m*n),肯定是Timeout。
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
for(int i=0; i<matrix.length; i++) {
for(int j=0; j<matrix[i].length; j++) {
if(matrix[i][j] == target) {
return true;
}
}
}
return false;
}
}
然后在优化的话就想到了二分。对每一行进行二分。时间复杂度是O(n*logm),还是Timeout,二分用的越多时间复杂度就越高,所以行列都二分(有点递归分治的意思)更会Timeout。
public class Solution {
public boolean binarySearch(int[] arr, int terget) {
int left = 0, int right = arr.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(target == arr[mid]) {
return true;
}
if(target > arr[mid]) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return false;
}
public boolean searchMatrix(int[][] matrix, int target) {
for(int i=0; i<matrix.length; i++) {
if(binarySearch(matrix[i], target)) {
return true;
}
}
return false;
}
}
这么看来时间复杂度必须在线性的基础上才行。观察一下矩阵不难发现把矩阵逆时针旋转45度类似一棵二叉查找树。所以就可以模仿二叉查找树的方法来做了。
这样的话时间复杂度就是O(m + n);AC。
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix.length==0 || matrix[0].length==0) {
return false;
}
int i = 0, j = matrix[0].length - 1;
while(i < matrix.length && j >= 0) {
int cur = matrix[i][j];
if(cur == target) {
return true;
}
else if(cur < target) {
i ++;
}
else {
j --;
}
}
return false;
}
}
LeetCode——Search a 2D Matrix II的更多相关文章
- [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 II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- 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】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
随机推荐
- # rp2833板卡更新u-boot.bin的步骤
1 建立tftpserver,并验证tftpserver的正确性(切记),并将PC主机网址设置192.168.18.105: 2 将u-boot-am.bincopy到tftpserver的目录下,并 ...
- Spring Session + Redis实现分布式Session共享
发表于 2016-09-29 文章目录 1. Maven依赖 2. 配置Filter 3. Spring配置文件 4. 解决Redis云服务Unable to configure Redis to k ...
- windows安装ruby,DevKit安装rails,svn安装
Ruby on Rails的安装,是从被称为RubyGems的包管理系统开始的.Ruby on Rails是由Ruby处理系统的类库的.被称为“gem”的格式来进行配置的.“gem”形式的类库,通过使 ...
- 虚拟IP和IP漂移
学习一下虚拟IP和IP漂移的概念. 1.虚拟IP 在 TCP/IP 的架构下,所有想上网的电脑,不论是用何种方式连上网路,都必须要有一个唯一的 IP-address.事实上IP地址是主机硬件地址的一种 ...
- thinkphp 前台测试
配置文件 <?php return array( 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 ...
- imx6 spi分析
/************************************************************************** *本文主要跟踪imx6 spi设备和驱动的注册过 ...
- Java JNI初探
---说明,之前直接百度出来的例子,照猫画虎.没想到的是这例子居然直接来自百度百科,写着写着就囧了.. ---anyway,写完了就当是给自己看吧. 同事求助,就看了一下,照猫画虎一番,略有所得. J ...
- linux命令详解之netstat
今天在使用linux的时候,要查看端口号,但是不知道要使用哪一个命令所以就学习了一下,原来是使用netstat,接下来给大家一起来学习. 一.netstat介绍 1.1.简介 Netstat 命令用于 ...
- (转)fiddler模拟post请求
转自:https://www.cnblogs.com/xiaoxi-3-/p/7612254.html 前言: Fiddler是一个简单的http协议调试代理工具,它界面友好,易于操作,是模拟http ...
- Unity5.5+easytouch5双摇杆控制角色移动
第一步:新建两个Joystick,分别改名LeftJoyStick和RightJoyStick 在LeftJoyStick的ETC Joystick-Axes properties中的Horizont ...