2
2
* Copyright (C) 2018, Saul Lawliet <october dot sunbathe at gmail dot com>
3
3
* All rights reserved.
4
4
*
5
+ * https://leetcode.com/problems/search-a-2d-matrix/
6
+ * Q: 在一个有序的二维数组中, 判断是否存在一个特定的值
7
+ *
5
8
* 转换一下矩阵下标, 同样是二分查找
6
9
*/
7
10
8
11
#include <stdbool.h>
9
12
#include "c/data-structures/array.h"
10
13
#include "c/test.h"
11
14
12
- bool searchMatrix (int * * matrix , int matrixRowSize , int matrixColSize , int target ) {
13
- int a = 0 , b = matrixRowSize * matrixColSize - 1 ;
15
+ bool searchMatrix (int * * matrix , int matrixSize , int * matrixColSize , int target ) {
16
+ int a = 0 , b = matrixSize * * matrixColSize - 1 ;
14
17
while (a <= b ) {
15
18
int i = (a + b ) / 2 ;
16
- int v = matrix [i / matrixColSize ][i % matrixColSize ];
19
+ int v = matrix [i / * matrixColSize ][i % * matrixColSize ];
17
20
if (v > target ) {
18
21
b = i - 1 ;
19
22
} else if (v < target ) {
@@ -28,7 +31,8 @@ bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target
28
31
void test (bool expect , const char * matrix , int target ) {
29
32
arrayEntry * e = arrayParse2D (matrix , ARRAY_INT );
30
33
31
- EXPECT_EQ_INT (expect , searchMatrix (arrayValue (e ), arrayRow (e ), arrayCol (e ), target ));
34
+ int col = arrayCol (e );
35
+ EXPECT_EQ_INT (expect , searchMatrix (arrayValue (e ), arrayRow (e ), & col , target ));
32
36
33
37
arrayFree (e );
34
38
}
0 commit comments