830. Positions of Large Groups
In a string S
of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like S = "abbxxxxzyy"
has the groups "a"
, "bb"
, "xxxx"
, "z"
and "yy"
.
Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group.
The final answer should be in lexicographic order.
考察点:一个指针遍历S,一个指针记录每个字符出现的次数。注意返回值
My method:
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** largeGroupPositions(char* S, int** columnSizes, int* returnSize) {
int i = 0, j;
int k = 0;
int **ret = (int **)malloc(sizeof(int *) * 334);
*columnSizes = (int *)malloc(sizeof(int) * 334);
*returnSize = 0;
while (S[i]) {
j = i + 1;
int ct = 1;
char tmp = S[i];
while (S[j] && (S[j] == tmp)) {
ct++;
j++;
}
if (ct >= 3) {
(*columnSizes)[k] = 2;
ret[k] = (int *)malloc(2 * sizeof(int));
ret[k][0] = i;
//printf("k=%d, ret[k][0]=%d\n", k, i);
ret[k][1] = j - 1;
//printf("k=%d, ret[k][1]=%d\n", k, j-1);
k++;
(*returnSize)++;
}
i = j;
}
return ret;
}
Submission Detail
202 / 202 test cases passed. | Status: Accepted |
Runtime: 4 ms | Submitted: 0 minutes ago |