当前位置:网站首页>Leetcode 240. search two-dimensional matrix II medium
Leetcode 240. search two-dimensional matrix II medium
2022-07-27 15:27:00 【Abcheche】
List of articles
1.Description
Write an efficient algorithm to search m x n matrix matrix One of the goals in target . The matrix has the following characteristics :
The elements of each row are arranged in ascending order from left to right .
The elements of each column are arranged in ascending order from top to bottom .
2.Example

Input :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]], target = 5
Output :true
3.Solution
1.
Start from the lower left corner of the matrix , If target>matrix[row][col] Just move to the right , If target<matrix[row][col] Just move up .
Time complexity :O(m+n)
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int row = m-1;
int col = 0;
while(row>=0&&col<=n-1) {
if(target>matrix[row][col]) {
col++;
}else if(target<matrix[row][col]) {
row--;
}else {
return true;
}
}
return false;
}
2. recursive
Divide the matrix into four parts , Upper left is less than target, The lower right is greater than target,target Included in the upper right and lower left , Recursively divide the upper right and lower left respectively .....
Time complexity :O(nlogn)
class Solution {
private int[][] matrix;
private int target;
private boolean searchRec(int left, int up, int right, int down) {
// this submatrix has no height or no width.
if (left > right || up > down) {
return false;
// `target` is already larger than the largest element or smaller
// than the smallest element in this submatrix.
} else if (target < matrix[up][left] || target > matrix[down][right]) {
return false;
}
int mid = left + (right-left)/2;
// Locate `row` such that matrix[row-1][mid] < target < matrix[row][mid]
int row = up;
while (row <= down && matrix[row][mid] <= target) {
if (matrix[row][mid] == target) {
return true;
}
row++;
}
return searchRec(left, row, mid-1, down) || searchRec(mid+1, up, right, row-1);
}
public boolean searchMatrix(int[][] mat, int targ) {
// cache input values in object to avoid passing them unnecessarily
// to `searchRec`
matrix = mat;
target = targ;
// an empty matrix obviously does not contain `target`
if (matrix == null || matrix.length == 0) {
return false;
}
return searchRec(0, 0, matrix[0].length-1, matrix.length-1);
}
}
边栏推荐
- 资本频频加码,急于上市的和府捞面有多“疯狂”?
- How "Crazy" is Hefu Laomian, which is eager to be listed, with capital increasing frequently?
- 网络设备硬核技术内幕 路由器篇 CISCO ASR9900拆解 (一)
- 网络设备硬核技术内幕 路由器篇 11 CISCO ASR9900拆解 (五)
- CAN总线的EMC设计方案
- USB interface electromagnetic compatibility (EMC) solution
- 3.3-5v转换
- 网络设备硬核技术内幕 路由器篇 19 DPDK(四)
- Overview of wechat public platform development
- 谷歌团队推出新Transformer,优化全景分割方案|CVPR 2022
猜你喜欢

How to edit a framework resource file separately

ad7606与stm32连接电路介绍

4种单片机驱动继电器方案

华云数据打造完善的信创人才培养体系 助力信创产业高质量发展

What is tor? What is the use of tor browser update?

Several basic uses of tl431-2.5v voltage reference chip
Principle of MOS tube to prevent reverse connection of power supply

After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues

Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81

LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard
随机推荐
Google team launches new transformer to optimize panoramic segmentation scheme CVPR 2022
Several basic uses of tl431-2.5v voltage reference chip
LeetCode 456. 132模式 单调栈/medium
Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
多表查询_练习1&练习2&练习3
Unity performance optimization ----- drawcall
DIY制作示波器的超详细教程:(一)我不是为了做一个示波器
《剑指Offer》数组中的逆序对
ad7606与stm32连接电路介绍
泛型
LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard
初探STM32掉电复位PDR
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
See "sense of security" in uncertainty Volvo asked in 2022
一些二进制位操作
Introduction of the connecting circuit between ad7606 and stm32
STM32之CAN ---CAN ID过滤器分析
STM32学习之CAN控制器简介
"Sword finger offer" linked list inversion
分布式锁