在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数
// vectorTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
using namespace std;
#include <vector>
class Solution {
public:
bool Find(vector<vector<int> > array, int target) {
int i = 0, j = 0;
int m = array.size();
int n = array[i].size();
bool isFound = false;
for (i = 0, j = n - 1; isFound == false&&j>=0&&i<m;)
{
if (target == array[i][j])
{
isFound = true;
break;
}
if (target < array[i][j])
j--;
else
i++;
}
cout << "isFound:" << isFound << endl;
return isFound;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<vector<int> > array = { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
Solution s;
s.Find(array, 6);
system("pause");
return 0;
}
//
#include "stdafx.h"
#include "iostream"
using namespace std;
#include <vector>
class Solution {
public:
bool Find(vector<vector<int> > array, int target) {
int i = 0, j = 0;
int m = array.size();
int n = array[i].size();
bool isFound = false;
for (i = 0, j = n - 1; isFound == false&&j>=0&&i<m;)
{
if (target == array[i][j])
{
isFound = true;
break;
}
if (target < array[i][j])
j--;
else
i++;
}
cout << "isFound:" << isFound << endl;
return isFound;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<vector<int> > array = { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
Solution s;
s.Find(array, 6);
system("pause");
return 0;
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。