mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Merge pull request #2835 from andyliao55555/master
[0739 每日温度] 增加C语言版本 [0496 下一个更大元素I] 增加C语言版本
This commit is contained in:
@ -195,6 +195,62 @@ public:
|
|||||||
建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简!
|
建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简!
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
### C
|
||||||
|
|
||||||
|
``` C
|
||||||
|
/* 先用单调栈的方法计算出结果,再根据nums1中的元素去查找对应的结果 */
|
||||||
|
/**
|
||||||
|
* Note: The returned array must be malloced, assume caller calls free().
|
||||||
|
*/
|
||||||
|
int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
|
||||||
|
|
||||||
|
/* stcak */
|
||||||
|
int top = -1;
|
||||||
|
int stack_len = nums2Size;
|
||||||
|
int stack[stack_len];
|
||||||
|
//memset(stack, 0x00, sizeof(stack));
|
||||||
|
|
||||||
|
/* nums2 result */
|
||||||
|
int* result_nums2 = (int *)malloc(sizeof(int) * nums2Size);
|
||||||
|
//memset(result_nums2, 0x00, sizeof(int) * nums2Size);
|
||||||
|
|
||||||
|
/* result */
|
||||||
|
int* result = (int *)malloc(sizeof(int) * nums1Size);
|
||||||
|
//memset(result, 0x00, sizeof(int) * nums1Size);
|
||||||
|
*returnSize = nums1Size;
|
||||||
|
|
||||||
|
/* init */
|
||||||
|
stack[++top] = 0; /* stack loaded with array subscripts */
|
||||||
|
|
||||||
|
for (int i = 0; i < nums2Size; i++) {
|
||||||
|
result_nums2[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* get the result_nums2 */
|
||||||
|
for (int i = 1; i < nums2Size; i++) {
|
||||||
|
if (nums2[i] <= nums2[stack[top]]) {
|
||||||
|
stack[++top] = i; /* push */
|
||||||
|
} else {
|
||||||
|
while ((top >= 0) && (nums2[i] > nums2[stack[top]])) {
|
||||||
|
result_nums2[stack[top]] = nums2[i];
|
||||||
|
top--; /* pop */
|
||||||
|
}
|
||||||
|
stack[++top] = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* get the result */
|
||||||
|
for (int i = 0; i < nums1Size; i++) {
|
||||||
|
for (int j = 0; j < nums2Size; j++) {
|
||||||
|
if (nums1[i] == nums2[j]) {
|
||||||
|
result[i] = result_nums2[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
```
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
@ -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:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
Reference in New Issue
Block a user