image.png
image.png
bool find(int* matrix, int rows, int columns, int number) {
bool found = false;
int row = 0;
int column = columns - 1;
if (matrix != nil && rows > 0 && columns > 0 ) {
while (row < rows && column < columns) {
int temp = matrix[row * columns + column];
if (temp == number) {
found = true;
break;
} else if (temp < number) {
row ++;
} else {
column --;
}
}
}
return found;
}
int main(int argc, const char * argv[]) {
int a[16] = {1,2,8,9, 2,4,9,12, 4,7,10,13, 6,8,11,15};
NSLog(@"%d", find(a, 4, 4, 7));
return 0;
}
网友评论