当前位置:网站首页>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;
}
}边栏推荐
- Exemple complet d'enregistrement du modèle pytoch + enregistrement du modèle pytoch seuls les paramètres d'entraînement sont - ils enregistrés? Oui (+ Solution)
- Share several map bed websites for everyone to share pictures
- Postman接口测试实战,这5个问题你一定要知道
- How to realize the function of detecting browser type in Web System
- I want to ask you, where is a better place to open an account in Dongguan? Is it safe to open a mobile account?
- 【每日一题】241. 为运算表达式设计优先级
- Automated video production
- [daily question] 241 Design priorities for operational expressions
- JASMINER X4 1U deep disassembly reveals the secret behind high efficiency and power saving
- In the era of consumer Internet, a few head platforms have been born
猜你喜欢

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

Customized Huawei hg8546m restores Huawei's original interface

勵志!大凉山小夥全獎直博!論文致謝看哭網友

「 工业缺陷检测深度学习方法」最新2022研究综述

CRM Customer Relationship Management System

CRM客户关系管理系统

SBT tutorial
In depth understanding of modern web browsers (I)

Talk about macromolecule coding theory and Lao Wang's fallacy from the perspective of evolution theory

【每日一题】241. 为运算表达式设计优先级
随机推荐
Google Earth engine (GEE) - Landsat 9 image full band image download (Beijing as an example)
What are the preferential account opening policies of securities companies now? Is it actually safe to open an account online?
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
Esp32c3 crash analysis
[daily question] 241 Design priorities for operational expressions
【Hot100】23. Merge K ascending linked lists
八年测开经验,面试28K公司后,吐血整理出高频面试题和答案
【Hot100】21. Merge two ordered linked lists
API文档工具knife4j使用详解
【Hot100】21. 合并两个有序链表
Attack and defense world PWN question: Echo
Cs5268 perfectly replaces ag9321mcq typec multi in one docking station solution
【Hot100】22. 括号生成
台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
Exemple complet d'enregistrement du modèle pytoch + enregistrement du modèle pytoch seuls les paramètres d'entraînement sont - ils enregistrés? Oui (+ Solution)
【实习】解决请求参数过长问题
Jetson XAVIER NX上ResUnet-TensorRT8.2速度與顯存記錄錶(後續不斷補充)
Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper
AcWing 341. Optimal trade solution (shortest path, DP)
Database schema notes - how to choose the right database in development + who invented relational database?