# 3.二维数组中的查找

Author posted @ 2013年9月30日 17:43 in 剑指Offer with tags c++ , 2659 阅读

$$\begin{bmatrix} 1 & 2 & 8 & 9 \\ 2 & 4& 9& 12\\ 4 & 7 & 10&13 \\ 6 & 8 & 11& 15\end{bmatrix}$$

bool find(int *matrix, int rows, int cols, int number){

if(matrix != NULL && rows >0 && cols > 0){
// search from lower left to the upper right
// start from bottom row
int current_row = rows - 1;
// start from first column
int current_col = 0;

while(current_row >=0 && current_col < cols){
if( matrix[current_row * cols + current_col] == number ){
cout<<"The number "<<number<<" is found ";
cout<<"at row ("<<current_row+1<<"), column ("<<current_col+1<<")"<<endl;
return true;
}else{
if(matrix[current_row *  cols + current_col] > number){
// go up and search above element in the same column
current_row--;
}else{
// go right and search element in the same row
current_col++;
}
}
}
}else{
cout<<"invalid matrix (NULL pointer passed) or out of range "<<endl;
return false;
}
return false;
}

git clone https://github.com/zhutiti/Hehaitao.git
seo 说:
2021年12月05日 10:45

Their https://www.digitekprinting.com/direct-mail are fast, efficient, and affordable. I can still remember the time when we needed a quick-print for a school project which was a magazine; they were able to help us print it because a lot of printing shops have declined since it is for only one copy, and printing shops usually print in bulk.

anonymous 说:
2023年7月09日 23:09

I would recommend my profile      is important to me, I invite you to discuss this topic. Wildlife Removal Kissimmee

anonymous 说:
2023年7月19日 17:29

I invite you to the page where     see how much we have in common. Orlando Wildlife Removal

anonymous 说:
2023年7月19日 17:32

I should assert barely that its astounding! The blog is informational also always fabricate amazing entitys. Wildlife Removal Oviedo

Cheap Windows VPS Ho 说:
2023年8月27日 22:22

VPS hosting stands for Virtual Private Server hosting, and it refers to a hosting solution where a physical server is divided into multiple virtual servers, each running its own operating system. In this case, the operating system used is Windows. <a href="https://cavpshost.com/ ">VPS hosting windows</a> provides greater control, privacy, and customization options compared to shared hosting, making it ideal for businesses or individuals with specific Windows-based hosting requirements.

VPS Hosting Windows 说:
2023年8月27日 22:23

VPS hosting stands for Virtual Private Server hosting, and it refers to a hosting solution where a physical server is divided into multiple virtual servers, each running its own operating system. In this case, the operating system used is Windows. VPS hosting windows provides greater control, privacy, and customization options compared to shared hosting, making it ideal for businesses or individuals with specific Windows-based hosting requirements.

anonymous 说:
2023年9月12日 04:35

I should assert barely that its astounding! The blog is informational also always fabricate amazing entitys. Hey Dude

anonymous 说:
2023年9月15日 17:15

The blog passage here is undeniably sublime and it should be shared at the site. The all inclusive community who read stuff on that site will genuinely welcome this post and read it and I trust that is useful for everybody.  flower delivery

anonymous 说:
2023年10月01日 18:26

I was failing and referred to this site. Honestly this was one of my greatest life decisions ever. Cannabis

(输入验证码)
or Ctrl+Enter