当前位置:网站首页>[leetcode] day103 search two-dimensional matrix II
[leetcode] day103 search two-dimensional matrix II
2022-07-24 15:57:00 【Upside down, it's a circle】
subject
240. Search for a two-dimensional matrix II【 secondary 】
Answer key
Two points search
My father, my fellow villagers , Next, we come to binary search in two-dimensional space !
It's actually a big name , The essence is binary search of one-dimensional array . Binary search for each row of the two-dimensional array , Finally get the target value .
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length,n=matrix[0].length;
// Line by line binary search
for(int[] row:matrix){
if(search(row,target))
return true;
}
return false;
}
// Two points search
public boolean search(int[] num,int target){
int low=0,high=num.length-1;
while(low<=high){
int mid=(low+high)/2;
if(num[mid]==target)
return true;
else if(num[mid]<target)
low=mid+1;
else
high=mid-1;
}
return false;
}
}
Time complexity : O ( m l o g n ) O(mlogn) O(mlogn)
Spatial complexity : O ( 1 ) O(1) O(1)
Z Font search
From matrix's Upper right corner Start ,
if matrix[x][y]> The target , The left one , namely y–;
if matrix[x][y]< The target , Move one down , namely x++;
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m=matrix.length,n=matrix[0].length;
int x=0,y=n-1;
while(x<m && y>=0){
int num=matrix[x][y];
if(num==target)
return true;
else if(num<target)
x++;// Move down
else
y--;// Move left
}
return false;
}
}
Time complexity : O ( m + n ) O(m+n) O(m+n)
Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- 【SWT】滚动容器实现商品列表样式
- 若依 this.$router.push 同地址不同参,页面不刷新问题
- Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password
- 【AdaptiveAvgPool3d】pytorch教程
- [shaders realize pixelate mosaic effect _shader effect Chapter 7]
- LaneATT
- JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue
- Introduction to single chip microcomputer: LED bidirectional water lamp
- Still using listview? Use animatedlist to make list elements move
- 矩阵的秩和图像的秩的一些了解
猜你喜欢

How to deal with being attacked? Advanced anti DDoS IP protection strategy

【AdaptiveAvgPool3d】pytorch教程

Using JS to implement click events

华为无线设备配置WAPI-证书安全策略

There are more than 20 categories of the four operators in MySQL. It took three days and three nights to sort them out. Don't collect them quickly

聊聊C指针

Small list of iptables common commands

Who is the "roll" king of the prefabricated vegetable track?

【着色器实现Pixelate马赛克效果_Shader效果第七篇】

Hard core innovation that database needs to care about in the future
随机推荐
Scala functions and their higher-order applications
22 bracket generation
应用修改日志路径log4j.properties
每天20分钟之feign
G026-db-gs-ins-03 openeuler deployment opengauss (1 active and 2 standby or multiple standby)
R语言ggplot2可视化:ggplot2可视化基本散点图(scatter plot)、通过在theme_bw中指定参数base_size来改变轴标签的大小、并控制网格线和轴标签的大小
3、 Set foundation ArrayList set and simple student management system
C - partial keyword
Will the capital market be optimistic about TCL's folding screen story?
JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
Introduction to bermudagrass
MySQL source code analysis -- data structure of index
Do you understand the working principle of gyroscope?
SQL row to column, column to row
LaneATT
kubernetes GPU的困境和破局
Leetcode 223. rectangular area
Yolov6 trains its own data set
20. Shell programming variables
AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘