[LC] 240. Search a 2D Matrix II
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.
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.
Solution 1:
Time: O(M * N)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
for (int[] arr: matrix) {
if (binarySearch(arr, target)) {
return true;
}
}
return false;
}
private boolean binarySearch(int[] array, int target) {
int start = 0, end = array.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (array[mid] == target) {
return true;
} else if (array[mid] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return false;
}
}
Solution 2:
Time: O(M + N)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
// check from upper right corner
int row = 0, col = matrix[0].length - 1;
while (col >= 0 && row <= matrix.length - 1) {
if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] < target) {
row += 1;
} else {
col -= 1;
}
}
return false;
}
}
[LC] 240. Search a 2D Matrix II的更多相关文章
- 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】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] 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】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 240. 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 ...
- (medium)LeetCode 240.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 240. 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 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
随机推荐
- css中标签总结
cursor CSS属性定义鼠标指针悬浮在元素上方显示的鼠标光标cursor:pointer: 小手 cursor:wait:等待....很多种 <span contenteditable=&q ...
- this 深度面试题3
window.val = 1; var obj = { val: 2, dbl: function () { this.val *= 2; val *= 2; console.log(val); co ...
- TX2Ubuntu16.04上安装 kinectV2
本文参考 https://www.ncnynl.com/archives/201706/1780.html 参考 https://blog.csdn.net/qq_33835307/arti ...
- Eclipse上传Git远程仓库,并且增加Maven Dependencies
前言: 遇见问题了,公司一台电脑,家里一台电脑,当有项目在进行的时候,又不想把电脑背来背去的,就像一个人玩单机,这个时候GIT就可以帮你解决这个问题.当GIT准备就绪的时候,新的问题来了git下载下载 ...
- python爬虫--cookie反爬处理
Cookies的处理 作用 保存客户端的相关状态 在爬虫中如果遇到了cookie的反爬如何处理? 手动处理 在抓包工具中捕获cookie,将其封装在headers中 应用场景:cookie没有有效时长 ...
- this关键字使用注意事项
1.当局部变量和成员变量重名时 ,java会启用就近原则,为了区分成员变量,最好再成员变量中加上this(this.成员变量),this的最主要的作用就是处理成员变量和局部变量重名的问题 例如,set ...
- 关于mysql数据库连接异常处理
tomcat启动错误日志关键信息: 28-Aug-2019 14:22:55.014 SEVERE [localhost-startStop-1] org.apache.catalina.core.C ...
- Jmeter学习前提:Jmeter安装
一.Jmeter下载 1. 前提:已经安装 jdk8+ 1.1 JDK下载 a. 进入jdk8+下载地址:https://www.oracle.com/technetwork/java/javase/ ...
- SpringBoot 1.5.x 集成 Quartz 任务调度框架
Quartz 有分 内存方式 和 数据库方式 内存方式任务信息保存在内存中, 停机会丢失, 需手动重新执行, 数据库方式: 任务信息保存在数据库中, 重点是支持集群. 内存方式 RAMJobStore ...
- Linux系统国内镜像站点
一,更换说明 第一步 备份 如centos, mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup ...