Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2d266b5

Browse files
committed
[0739 temperature] add C version
add C version for 0739: temperature Signed-off-by: liao junwu <andyliaowu5@163.com>
1 parent 1c82540 commit 2d266b5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

problems/0739.每日温度.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,38 @@ public:
215215

216216
## 其他语言版本
217217

218+
### C:
219+
220+
```C
221+
/**
222+
* Note: The returned array must be malloced, assume caller calls free().
223+
*/
224+
int* dailyTemperatures(int* temperatures, int temperaturesSize, int* returnSize) {
225+
int len = temperaturesSize;
226+
*returnSize = len;
227+
228+
int *result = (int *)malloc(sizeof(int) * len);
229+
memset(result, 0x00, sizeof(int) * len);
230+
231+
int stack[len];
232+
memset(stack, 0x00, sizeof(stack));
233+
int top = 0;
234+
235+
for (int i = 1; i < len; i++) {
236+
if (temperatures[i] <= temperatures[stack[top]]) { /* push */
237+
stack[++top] = i;
238+
} else {
239+
while (top >= 0 && temperatures[i] > temperatures[stack[top]]) { /* stack not empty */
240+
result[stack[top]] = i - stack[top];
241+
top--; /* pop */
242+
}
243+
stack[++top] = i; /* push */
244+
}
245+
}
246+
return result;
247+
}
248+
```
249+
218250
### Java:
219251
220252
```java

0 commit comments

Comments
 (0)