当前位置:网站首页>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 Report on the overall scale, major manufacturers, major regions, products and application segmentation of multi-channel signal conditioners in the global market in 2022
- Review of the latest 2022 research on "deep learning methods for industrial defect detection"
- 【实习】解决请求参数过长问题
- 在网上炒股开户安全吗?我是新手,还请指导
- Research Report on the overall scale, major manufacturers, major regions, products and applications of hollow porcelain insulators in the global market in 2022
- Don't you want to have a face-to-face communication with cloud native and open source experts? (including benefits
- Google Earth engine (GEE) - Landsat 9 image full band image download (Beijing as an example)
- Research Report on the overall scale, major manufacturers, major regions, products and applications of battery control units in the global market in 2022
- sql-labs
- 【Hot100】22. 括号生成
猜你喜欢
Data preparation for behavior scorecard modeling
Jetson XAVIER NX上ResUnet-TensorRT8.2速度與顯存記錄錶(後續不斷補充)
Spark source code compilation, cluster deployment and SBT development environment integration in idea
AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
面试经验总结,为你的offer保驾护航,满满的知识点
Detailed upgrade process of AWS eks
Codeforces round 651 (Div. 2) (a thinking, B thinking, C game, D dichotomy, e thinking)
Use graalvm native image to quickly expose jar code as a native shared library
How can testers do without missing tests? Seven o'clock is enough
[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process
随机推荐
Is it safe to open an account for online stock speculation? I'm a novice, please guide me
Detailed upgrade process of AWS eks
Send blessings on Lantern Festival | limited edition red envelope cover of audio and video is released!
For (Auto A: b) and for (Auto & A: b) usage
[cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps
勵志!大凉山小夥全獎直博!論文致謝看哭網友
Data preparation for behavior scorecard modeling
Cs5268 perfectly replaces ag9321mcq typec multi in one docking station solution
esp32c3 crash分析
JS modularization
分享几个图床网址,便于大家分享图片
【871. 最低加油次数】
Esp32c3 crash analysis
upload-labs
In the era of consumer Internet, a few head platforms have been born
1007 maximum subsequence sum (25 points) "PTA class a exercise"
I did a craniotomy experiment: talk about macromolecule coding theory and Lao Wang's fallacy from corpus callosum and frontal leukotomy
Sometimes only one line of statements are queried, and the execution is slow
API documentation tool knife4j usage details
[internship] solve the problem of too long request parameters