Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Dec 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[0496 the next bigger element] add C version
add C version for 0496: the next bigger element
Signed-off-by: liao junwu <andyliaowu5@163.com>
  • Loading branch information
359023320 committed Nov 16, 2024
commit da88f5e4c1407d99b46f087a6d5d10f6e685844e
56 changes: 56 additions & 0 deletions problems/0496.下一个更大元素I.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

AltStyle によって変換されたページ (->オリジナル) /