-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1337.矩阵中战斗力最弱的-k-行.c
78 lines (53 loc) · 2.05 KB
/
1337.矩阵中战斗力最弱的-k-行.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* @lc app=leetcode.cn id=1337 lang=c
*
* [1337] 矩阵中战斗力最弱的 K 行
*/
// @lc code=start
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* kWeakestRows(int** mat, int matSize, int* matColSize, int k, int* returnSize){
int *resultVector = malloc(sizeof(int) * k);
int *assistantVector = malloc(sizeof(int) * matSize);
int *assistantIndexVector = malloc(sizeof(int) * matSize);
memset(assistantVector, 0, (sizeof(int) * matSize));
for (int i = 0; i < matSize; i++) {
for (int j = 0; j < (*matColSize); j++) {
if (mat[i][j] == 1) {
assistantVector[i]++;
}
else {
break;
}
}
assistantIndexVector[i] = i;
// printf ("%d, %d, %d, \n", i, assistantVector[i], assistantIndexVector[i]);
}
for (int i = 0; i < matSize; i++) {
int targetIndex = i;
for (int j = (targetIndex + 1); j < matSize; j++) {
if (assistantVector[targetIndex] > assistantVector[j]) {
targetIndex = j;
}
else if ((assistantVector[targetIndex] == assistantVector[j]) && (assistantIndexVector[targetIndex] > assistantIndexVector[j])) {
targetIndex = j;
}
}
if (targetIndex != i) {
assistantVector[targetIndex] ^= assistantVector[i];
assistantVector[i] ^= assistantVector[targetIndex];
assistantVector[targetIndex] ^= assistantVector[i];
assistantIndexVector[i] ^= assistantIndexVector[targetIndex];
assistantIndexVector[targetIndex] ^= assistantIndexVector[i];
assistantIndexVector[i] ^= assistantIndexVector[targetIndex];
}
// printf ("%d, %d, \n", assistantIndexVector[i], assistantIndexVector[targetIndex]);
if (i < k) {
resultVector[i] = assistantIndexVector[i];
}
}
(*returnSize) = k;
return resultVector;
}
// @lc code=end