### 面试题5：二维数组中的查找

1 2 8 9
2 4 9 12
4 7 9 13
6 8 11 15

#### 2.思路

（1）暴力
（2）借助二维数组左右有序，上下有序的性质减少查找区域

#### 3.code

```#include <iostream>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

bool force_find(vector<vector<int>> vec, int target)
{
for(int i = 0; i < vec.size(); ++i){
for(int j = 0; j < vec[i].size(); ++j){
if(target == vec[i][j]){
return true;
}
}
}
return false;
}

bool decrease_find(vector<vector<int>> vec, int target)
{
int row = vec.size();
int col = vec[0].size();
int i = 0;
int j = col-1;
while(i < row && j >= 0){
if(vec[i][j] == target){
return true;
}
else if(vec[i][j] > target){
--j;
}
else
++i;
}
return false;
}

int main()
{
vector<vector<int>> vec;
vector<int> v1 = {1,2,8,9};
vector<int> v2 = {2,4,9,12};
vector<int> v3 = {4,7,9,13};
vector<int> v4 = {6,8,11,15};
vec.push_back(v1);
vec.push_back(v2);
vec.push_back(v3);
vec.push_back(v4);

int target1 = 7;
int target2 = 5;
bool flag1 = force_find(vec,target1);
bool flag2 = decrease_find(vec,target2);
cout << target1 << ": " << flag1 << endl;
cout << target2 << ": " << flag2 << endl;
return 0;
}```