-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1380.矩阵中的幸运数.c
More file actions
57 lines (38 loc) · 1.38 KB
/
1380.矩阵中的幸运数.c
File metadata and controls
57 lines (38 loc) · 1.38 KB
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
/*
* @lc app=leetcode.cn id=1380 lang=c
*
* [1380] 矩阵中的幸运数
*/
// @lc code=start
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* luckyNumbers (int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
int *assistantVector = malloc(sizeof(int) * matrixSize);
int *assistantColVector = malloc(sizeof(int) * (*matrixColSize));
int *resultVector = malloc(sizeof(int) * (matrixSize));
memset(assistantVector, 0, (sizeof(int) * matrixSize));
memset(assistantColVector, 0, (sizeof(int) * (*matrixColSize)));
memset(resultVector, 0, (sizeof(int) * (matrixSize)));
for (int i = 0; i < matrixSize; i++) {
for (int j = 0; j < (*matrixColSize); j++) {
if (matrix[i][j] < matrix[i][assistantVector[i]]) {
assistantVector[i] = j;
}
if (matrix[i][j] > matrix[assistantColVector[j]][j]) {
assistantColVector[j] = i;
}
}
}
int resultCounter = 0;
for (int i = 0; i < matrixSize; i++) {
for (int j = 0; j < (*matrixColSize); j++) {
if ((assistantVector[i] == j) && (assistantColVector[j] == i)) {
resultVector[resultCounter++] = matrix[i][j];
}
}
}
(*returnSize) = resultCounter;
return resultVector;
}
// @lc code=end