Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7372a86

Browse files
work in progress
1 parent 595cb7c commit 7372a86

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

‎c/000-099/74-search-a-2d-matrix.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
* Copyright (C) 2018, Saul Lawliet <october dot sunbathe at gmail dot com>
33
* All rights reserved.
44
*
5+
* https://leetcode.com/problems/search-a-2d-matrix/
6+
* Q: 在一个有序的二维数组中, 判断是否存在一个特定的值
7+
*
58
* 转换一下矩阵下标, 同样是二分查找
69
*/
710

811
#include <stdbool.h>
912
#include "c/data-structures/array.h"
1013
#include "c/test.h"
1114

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;
1417
while (a <= b) {
1518
int i = (a + b) / 2;
16-
int v = matrix[i / matrixColSize][i % matrixColSize];
19+
int v = matrix[i / *matrixColSize][i % *matrixColSize];
1720
if (v > target) {
1821
b = i - 1;
1922
} else if (v < target) {
@@ -28,7 +31,8 @@ bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target
2831
void test(bool expect, const char *matrix, int target) {
2932
arrayEntry *e = arrayParse2D(matrix, ARRAY_INT);
3033

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));
3236

3337
arrayFree(e);
3438
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /