-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffer-04-FindInPartiallySortedMatrix.c
61 lines (54 loc) · 1.09 KB
/
offer-04-FindInPartiallySortedMatrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdbool.h>
#include "minunit.h"
bool findNumberIn2DArray(int **matrix, int matrixSize, int *matrixColSize, int target)
{
if (matrix == NULL || matrixSize == 0 || *matrixColSize == 0)
{
return false;
}
for (int row = 0, col = *matrixColSize - 1; row < matrixSize && col >= 0;)
{
if (target < matrix[row][col])
{
col--;
}
else if (target > matrix[row][col])
{
row++;
}
else
{
return true;
}
}
return false;
}
MU_TEST(test_case)
{
int a[5][5] = {
{1, 4, 7, 11, 15},
{2, 5, 8, 12, 19},
{3, 6, 9, 16, 22},
{10, 13, 14, 17, 24},
{18, 21, 23, 26, 30}};
int cols = 5;
// https://cloud.tencent.com/developer/article/1451114, array in function parameters
int *p[5];
for (int i = 0; i < 5; i++)
{
p[i] = &a[i][0];
}
mu_check(true == findNumberIn2DArray(p, 5, &cols, 5));
mu_check(false == findNumberIn2DArray(p, 5, &cols, 20));
}
MU_TEST_SUITE(test_suite)
{
MU_RUN_TEST(test_case);
}
int main()
{
MU_RUN_SUITE(test_suite);
MU_REPORT();
return MU_EXIT_CODE;
}