当前位置:网站首页>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);
}
}
边栏推荐
- Unity's simplest object pool implementation
- Several basic uses of tl431-2.5v voltage reference chip
- Usage of countdownlatch in multithreaded environment
- EMC design scheme of RS485 interface
- Network equipment hard core technology insider router Chapter 5 tompkinson roaming the network world (Part 1)
- 网络设备硬核技术内幕 路由器篇 14 从鹿由器到路由器 (中)
- LeetCode 81. 搜索旋转排序数组 II 二分/medium
- Stm32f103c8t6 drives ssd1306 0.96 "IIC OLED display under Arduino frame
- USB接口电磁兼容(EMC)解决方案
- DIY制作示波器的超详细教程:(一)我不是为了做一个示波器
猜你喜欢

Unity 鼠标控制第一人称摄像机视角

Design scheme of digital oscilloscope based on stm32

JUC(JMM、Volatile)

仅做两项修改,苹果就让StyleGANv2获得了3D生成能力

How to package AssetBundle

3.3-5v conversion

LeetCode 781. 森林中的兔子 哈希表/数学问题 medium

Several basic uses of tl431-2.5v voltage reference chip

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

谷歌团队推出新Transformer,优化全景分割方案|CVPR 2022
随机推荐
STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
Unity性能优化------渲染优化(GPU)之Occlusion culling(遮挡剔除)
STM32 can communication filter setting problem
适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
Dialog manager Chapter 3: create controls
RS485接口的EMC设计方案
With just two modifications, apple gave styleganv2 3D generation capabilities
lua学习笔记
Unity最简洁的对象池实现
LeetCode 1143. 最长公共子序列 动态规划/medium
Adaptation verification new occupation is coming! Huayun data participated in the preparation of the national vocational skill standard for information system adaptation verifiers
反射
Leetcode 244周赛-赛后补题题解【西兰花选手】
DevEco Studio2.1运行项目报错
generic paradigm
EMC design scheme of RS485 interface
LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard
The first common node of the two linked lists of "Jianzhi offer"
Usage of countdownlatch in multithreaded environment
IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items