#include <stdio.h>
/**
杨氏矩阵 - 剑指 offer
题目内容:
有一个数字矩阵,矩阵的每行从左到右是递增的,矩阵从上到下是递增的,请编写程序在这样的矩阵中查找某个数字是否存在
要求:
事件复杂程度小于 O(N)
*/
int main() {
int arr[3][3] = { {1,2,3},{2,3,4},{3,4,5} };
int res = findNumber(arr , 4 ,3 , 3);
if (res)
printf("存在");
else
printf("不存在");
return 0;
}
int findNumber(int (*arr)[3], int k, int row, int col) {
int x = 0;
int y = col - 1;
while (x < row - 1 && y >= 0) {
if (arr[x][y] > k) {
y--;
}
else if (arr[x][y] > k) {
x++;
}
else {
return 1;
}
}
return 0;
}
杨氏矩阵
最新推荐文章于 2024-02-04 13:32:17 发布