您的位置:首页 > 其它

从行,列均为递增的二维数组中查找给定元素

2013-09-17 12:23 169 查看
如题:

设计算法

bool Find(int *maxtrix, const int rows, const int cols, const int key)
{
if (maxtrix == NULL || rows < 0 || cols < 0) {
return false;
}

bool found = false;
int row = 0, col = cols - 1;

while (row < rows && col >= 0) {
if (maxtrix[row * cols + col] == key) {
found = true;
break;
} else if (maxtrix[row * cols + col] > key) {
-- col;
} else {
++ row;
}
}

return found;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐