当前位置:网站首页>Sword finger offer (II) -- search in two-dimensional array
Sword finger offer (II) -- search in two-dimensional array
2022-07-02 20:35:00 【Qinhuai grocery store】
Title Description
In a two-dimensional array ( Each one-dimensional array has the same length ), Each row is sorted in ascending order from left to right , Each column is sorted in ascending order from top to bottom . Please complete a function , Enter such a two-dimensional array and an integer , Determine whether the array contains the integer .
Example
Enter an array :
num[3][4]=[ 1,4,6,28, 2,7,32,30, 10,11,67,79 ]
You need to find a number 32, Then return to true
Ideas
You can go through it directly , But this complexity in the worst case is to facilitate all to get the results . If the array size is n×m, So the complexity is O(n×m). But let's think another way , We chose the one in the lower left corner 10(num[2][0],i=2,j=0) As a starting point , If it is greater than 10, that i+1, If it is less than 10, be j+1, The next number to look up is 11, We know 32 Still more than 11 Big , To the right 67, then 32 Than 67 Small , We should look up , eureka 32. If you look for 28, It's the worst , Find the end of the upper right corner of the array , thus , The worst result is O(n+m).
Code
public class Solution { public boolean Find(int target, int [][] array) { int size=array.length; int length=array[0].length; int i,j; for(i=size-1,j=0;i>=0&&i<size&&j>=0&&j<length;){ if(array[i][j]==target){ return true;
} if(array[i][j]<target){
j++; continue;
} if(array[i][j]>target){
i--; continue;
}
} return false;
}
}边栏推荐
- Research and Analysis on the current situation of China's clamping device market and forecast report on its development prospect
- 【Hot100】23. Merge K ascending linked lists
- Spark source code compilation, cluster deployment and SBT development environment integration in idea
- 【QT】QPushButton创建
- 笔记本安装TIA博途V17后出现蓝屏的解决办法
- Research Report on the overall scale, major manufacturers, major regions, products and applications of micro hydraulic cylinders in the global market in 2022
- GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
- 【实习】解决请求参数过长问题
- I would like to ask what securities dealers recommend? Is it safe to open a mobile account?
- One side is volume, the other side is layoff. There are a lot of layoffs in byte commercialization department. What do you think of this wave?
猜你喜欢

【Hot100】21. 合并两个有序链表

upload-labs

Jetson XAVIER NX上ResUnet-TensorRT8.2速度與顯存記錄錶(後續不斷補充)
![[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process](/img/e6/1dc747de045166f09ecdce1c5a34b1.jpg)
[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process

Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper

Postman接口测试实战,这5个问题你一定要知道

Redis sentinel cluster working principle and architecture deployment # yyds dry goods inventory #

Driverless learning (4): Bayesian filtering
![[QT] QPushButton creation](/img/c4/bc0c346e394484354e5b9d645916c0.png)
[QT] QPushButton creation

【每日一题】241. 为运算表达式设计优先级
随机推荐
Google Earth Engine(GEE)——Landsat 9影像全波段影像下载(北京市为例)
Talk about macromolecule coding theory and Lao Wang's fallacy from the perspective of evolution theory
CRM客户关系管理系统
数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?
Backpack template
Implementing yolox from scratch: dataset class
Is it safe to buy funds on securities accounts? Where can I buy funds
想请教一下,我在东莞,到哪里开户比较好?手机开户是安全么?
Interested parties add me for private chat
How to do interface testing? After reading this article, it will be clear
笔记本安装TIA博途V17后出现蓝屏的解决办法
An analysis of the past and present life of the meta universe
Research Report on the overall scale, major manufacturers, major regions, products and applications of micro hydraulic cylinders in the global market in 2022
在网上炒股开户安全吗?我是新手,还请指导
Solution to blue screen after installing TIA botu V17 in notebook
AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion
【871. 最低加油次数】
1005 spell it right (20 points) "PTA class a exercise"