当前位置:网站首页>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; } }
边栏推荐
- 在网上炒股开户安全吗?我是新手,还请指导
- 攻防世界pwn题:Recho
- Why do I have a passion for process?
- [cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps
- 【871. 最低加油次数】
- HDL design peripheral tools to reduce errors and help you take off!
- 【Hot100】23. Merge K ascending linked lists
- Don't you want to have a face-to-face communication with cloud native and open source experts? (including benefits
- 【Hot100】21. 合并两个有序链表
- Data preparation for behavior scorecard modeling
猜你喜欢
Cs5268 perfectly replaces ag9321mcq typec multi in one docking station solution
After eight years of test experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers
CRM Customer Relationship Management System
sql-labs
The metamask method is used to obtain account information
API文档工具knife4j使用详解
[cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps
Complete example of pytorch model saving +does pytorch model saving only save trainable parameters? Yes (+ solution)
Database schema notes - how to choose the right database in development + who invented relational database?
Detailed upgrade process of AWS eks
随机推荐
Google Earth Engine(GEE)——Landsat 9影像全波段影像下载(北京市为例)
面试经验总结,为你的offer保驾护航,满满的知识点
【JS】获取hash模式下URL的搜索参数
【871. 最低加油次数】
pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
Basic concept of database, installation and configuration of database, basic use of MySQL, operation of database in the project
Send blessings on Lantern Festival | limited edition red envelope cover of audio and video is released!
Spark source code compilation, cluster deployment and SBT development environment integration in idea
Research Report on the overall scale, major manufacturers, major regions, products and applications of hollow porcelain insulators in the global market in 2022
How my mother-in-law and daughter-in-law get along
Implementing yolox from scratch: dataset class
What are the preferential account opening policies of securities companies now? Is it actually safe to open an account online?
Outsourcing for three years, abandoned
Resunnet - tensorrt8.2 Speed and Display record Sheet on Jetson Xavier NX (continuously supplemented)
[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process
【每日一题】241. 为运算表达式设计优先级
The first of the classic quotations of correspondents is heartbreaking
Roommate, a king of time, I took care of the C language structure memory alignment
自动化制作视频
八年测开经验,面试28K公司后,吐血整理出高频面试题和答案