mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
[0739 temperature] add C version
add C version for 0739: temperature Signed-off-by: liao junwu <andyliaowu5@163.com>
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user