# 剑指offer（3）二维数组的查找

#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
bool Find(int target, vector<vector<int> > array)
{

int rows = array.size();//行数
int cols = array[0].size();//列数
if (rows==0&&cols==0)
{
return false;
}
int row = 0;
int col = cols - 1;
while (row < rows&&col >= 0)
{
if (array[row][col] == target)//判断是否是预期值，是的话返回true，结束程序
{
return true;
break;
}
else if (array[row][col] > target)//如果数组右上角的数大于预期值，则去掉当前列，否则增加当前行
{
--col;
}
else
++row;
}
return false;
}
};
int _tmain()
{
vector <vector<int>> array;//定义一个二维数组
int target;
vector<int> v;//建立一个vector
int temp;
int rows;
cout << "input array rows: ";
cin >> rows;
int cols;
cout << "input array cols: ";
cin >> cols;
cout << "input array element: " << endl;
for (int i = 0;i<rows;i++)
{
v.clear();
for (int j = 0;j<cols;j++)
{
cin >> temp;
v.push_back(temp);//把输入的数压入vector，尾部插入数字：vec.push_back(a);
}
array.push_back(v);
}
cout << "input target";
cin >> target;
Solution s;
if (s.Find(target, array))
cout << "YES";
else
cout << "NO";
system("pause");
return 0;
}