力扣 2055 /// 蜡烛之间的盘子
地址: https://leetcode-cn.com/problems/plates-between-candles/ int* platesBetweenCandles(char * s, int** queries, int queriesSize, int* queriesColSize, int* returnSize){ int n = strlen(s); int * preSum = (int )malloc(sizeof(int) * n); memset(preSum, 0, sizeof(int) * n); for (int i = 0, sum = 0; i < n; i++) { if (s[i] == '’) { sum++; } preSum[i] = sum; } int * left = (int *)malloc(sizeof(int) * n); memset(left, 0, sizeof(int) * n); for (int i = 0, l = -1; i < n; i++) { if (s[i] == ‘|’) { l = i; } left[i] = l; } int * right = (int *)malloc(sizeof(int) * n); memset(right, 0, sizeof(int) * n); for (int i = n - 1, r = -1; i >= 0; i–) { if (s[i] == ‘|’) { r = i; } right[i] = r; } int * ans = (int *)malloc(sizeof(int) * queriesSize); for (int i = 0; i < queriesSize; i++) { int x = right[queries[i][0]], y = left[queries[i][1]]; ans[i] = x == -1 || y == -1 || x >= y ? 0 : preSum[y] - preSum[x]; } free(preSum); free(left); free(right); *returnSize = queriesSize; return ans; }
|