[0739 temperature] add C version

add C version for 0739: temperature

Signed-off-by: liao junwu <andyliaowu5@163.com>
This commit is contained in:
liao junwu
2024-11-14 23:24:17 +08:00
parent 1c8254045c
commit 2d266b5766

View File

@ -215,6 +215,38 @@ public:
## 其他语言版本
### C
```C
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* dailyTemperatures(int* temperatures, int temperaturesSize, int* returnSize) {
int len = temperaturesSize;
*returnSize = len;
int *result = (int *)malloc(sizeof(int) * len);
memset(result, 0x00, sizeof(int) * len);
int stack[len];
memset(stack, 0x00, sizeof(stack));
int top = 0;
for (int i = 1; i < len; i++) {
if (temperatures[i] <= temperatures[stack[top]]) { /* push */
stack[++top] = i;
} else {
while (top >= 0 && temperatures[i] > temperatures[stack[top]]) { /* stack not empty */
result[stack[top]] = i - stack[top];
top--; /* pop */
}
stack[++top] = i; /* push */
}
}
return result;
}
```
### Java
```java