当前位置:网站首页>"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

边栏推荐
- CloudCompare&PCL 点云SVD分解
- WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持
- Why are function templates not partial specialization?
- Build an integrated kubernetes in Fedora
- Comment supprimer le crosstalk SiC MOSFET?
- Idea related issues
- Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
- centos mysql5.5配置文件在哪
- Selenium+chromedriver cannot open Google browser page
- 隐私计算FATE-----离线预测
猜你喜欢

Protection range and optimization of motor protector for hoist equipment
![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)
[untitled]

使用transform:scale之后导致页面鼠标悬浮事件消失

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

Wasmedge 0.10.0 release! New plug-in extension mechanism, socket API enhancement, llvm 14 support

Selenium+chromedriver cannot open Google browser page

AWS builds a virtual infrastructure including servers and networks (2)

用Pytorch搭建第一個神經網絡且進行優化

Using transform:scale causes the page mouse hover event to disappear

Idea related issues
随机推荐
Dell r730 server startup error: [xxx] USB 1-1-port4: disabled by hub (EMI?), re-enabling...
High rise building fire prevention
[go ~ 0 to 1] the next day, June 25, switch statement, array declaration and traversal
[untitled]
Sword finger offer 30 Stack containing min function
【力扣10天SQL入门】Day5+6 合并表
Large current and frequency range that can be measured by Rogowski coil
The 6th smart home Asia 2022 will be held in Shanghai in October
Guangzhou: new financial activities and new opportunities for enterprises
Copy & Deepcopy
Application of energy management system in iron and steel enterprises
[go ~ 0 to 1] the third day June 27 slice, map and function
Using transform:scale causes the page mouse hover event to disappear
CloudCompare&PCL 点云SVD分解
Lilda low code data large screen, leveling the threshold of data application development
STL -- binder
A - 深海探险
Selenium+chromedriver cannot open Google browser page
Build an integrated kubernetes in Fedora
Integer partition