当前位置:网站首页>"Jianzhi offer" -- Interview Question 4: finding two-dimensional arrays
"Jianzhi offer" -- Interview Question 4: finding two-dimensional arrays
2022-06-28 08:50:00 【Jinky strange 18】
In a two-dimensional array ( Each one-dimensional array has the same length , That is, the column is fixed ), 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 .

Increment to line , A two-dimensional array of incrementing columns Search for
*** The first method ***
One That's ok One That's ok In the process of Compare , If the following value is greater than the lookup value , Just turn To The next line Compare , And the data in the following columns do not need to be compared
#include<iostream>
#define M 4
int Search(int(*ar)[M],int col,int key)
{
int row = M;
for(int i = 0;i < col;i++)
{
for(int j = 0;j < row;j++)
{
if(key == ar[i][j])
{
return ar[i][j];
}
else if(key < ar[i][j])
// notes : When the value of a column is greater than key when , There is no need to compare the following columns
{
row = j;// At this time will be j Assign to row
}
}
}
return false;
}
int main()
{
int ar[][M] = {
{1,2,8,14},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
int col = sizeof(ar)/sizeof(ar[0]);
int key;
std::cin >> key;
if(Search(ar,col,key))
{
std::cout << " Yes " << key << " This number " <<std::endl;
}
else
{
std::cout << " No, " << key << " This number " <<std::endl;
}
return 0;
}*** The second method ***
Based on the first method , The search in each row uses Two points search ( For an ordered sequential table, first consider using binary search , The advantage is fast , The processing effect of massive data is more obvious )
#include<iostream>
#define M 4
int BinSearch(int *ar,int len,int key)
{
int low = 0;
int high = len - 1;
int mid;
while(low <= high)
{
mid = (high + low) >> 1;
if(key == ar[mid])
{
return mid;
}
else if(key > ar[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return mid;
}
int Search(int(*ar)[M],int col,int key)
{
int index = col - 1;
for(int i = 0;i < M;i++)
{
index = BinSearch(ar[i],index+1,key);
if(index < 0)
{
return false;
}
if(ar[i][index] == key)
{
return ar[i][index];
}
}
return false;
}
int main()
{
int ar[][M] = {
{1,2,8,14},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
int col = sizeof(ar)/sizeof(ar[0]);
int key;
std::cin >> key;
if(Search(ar,col,key))
{
std::cout << " Yes " << key << " This number " <<std::endl;
}
else
{
std::cout << " No, " << key << " This number " <<std::endl;
}
return 0;
}
The test case
* The two-dimensional array contains the number to look up
Maximum 15
minimum value 1
A number between maximum and minimum 9
* There are no numbers to look up in a two-dimensional array
Greater than the maximum 20
Less than the minimum -3
A number between the maximum and the minimum but without 5
* Special input test
NULL

边栏推荐
猜你喜欢

Operating principle of Rogowski coil

叠加阶梯图和线图及合并线图和针状图

Love analysis released the 2022 love analysis · it operation and maintenance manufacturer panorama report, and an Chao cloud was strongly selected!
![DELL R730服务器开机报错:[XXX] usb 1-1-port4: disabled by hub (EMI?), re-enabling...](/img/90/425965ca4b3df3656ce2a5f4230c4b.jpg)
DELL R730服务器开机报错:[XXX] usb 1-1-port4: disabled by hub (EMI?), re-enabling...

罗氏线圈可以测量的大电流和频率范围

Matlab tips (20) matrix analysis -- principal component regression

第六届智能家居亚洲峰会暨精品展(Smart Home Asia 2022)将于10月在沪召开

爱分析发布《2022爱分析 · IT运维厂商全景报告》 安超云强势入选!

containerd1.5.5的安装

Discussion on safety management of centralized maintenance construction site of substation under the mode of operation and maintenance
随机推荐
Application of current limiting protector in preventing electrical fire in shopping malls
FatMouse and Cheese
Key points of building fire protection design
【力扣10天SQL入门】Day5+6 合并表
Selenium reptile
利尔达低代码数据大屏,铲平数据应用开发门槛
个人究竟如何开户炒股?在线开户安全么?
FatMouse and Cheese
break database---mysql
Mysql8.0 forgot the root password
Wasmedge 0.10.0 release! New plug-in extension mechanism, socket API enhancement, llvm 14 support
Sword finger offer 30 Stack containing min function
Super Jumping! Jumping! Jumping!
Kubernetes notes and the latest k3s installation introduction
Quelle est la largeur de bande du serveur de bavardage sonore pour des centaines de millions de personnes en même temps?
Kali Notes(1)
Comment supprimer le crosstalk SiC MOSFET?
yaml json
Loss损失函数
MATLAB小技巧(20)矩阵分析--主成分回归