博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
17.7.7
阅读量:7107 次
发布时间:2019-06-28

本文共 545 字,大约阅读时间需要 1 分钟。

七七事变日

剑指offer:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

vector也可以用下标来访问,成员函数v.size()返回容器包含的元素个数。注意此处多维的疑惑

错误见注释

最终版

class Solution {

public:
bool Find(int target, vector<vector<int> > array) {
int col = array[0].size() - 1;//此处出错,应该 - 1否则会数组越界
int row = array.size();
int i = 0;
while(i < row && col >= 0) {
if(array[i][col] == target)
return true;
else if(array[i][col] > target) {
--col;
continue;
}
else{
++i;
continue;
}
}
return false;
}
};

转载于:https://www.cnblogs.com/bloomingFlower/p/7133152.html

你可能感兴趣的文章