暴力
int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
int i = 0;
int j = 0;
int flag = 0;
int *ans;
ans = (int *)malloc(nums1Size * sizeof(int));
for (i = 0; i < nums1Size; i++) {
flag = 0;
for (j = 0; j < nums2Size; j++) {
if(nums1[i] == nums2[j]) {
flag = 1;
}
if ((flag == 1) && (nums1[i] < nums2[j])) {
ans[i] = nums2[j];
flag = 2;
break;
}
}
if (flag != 2) {
ans[i] = -1;
}
}
*returnSize = nums1Size;
return ans;
}
栈+map
int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
int sstack[nums2Size];
int top = 0;
int map[10005] = {0};
int *ans;
ans = (int*)malloc(nums1Size * sizeof(int));
sstack[top] = nums2[0];
for (int i = 1; i < nums2Size; i++) {
while ((top >= 0) && (sstack[top] < nums2[i])) {
map[sstack[top]] = nums2[i];
top--;
}
top++;
sstack[top] = nums2[i];
}
for (int i = 0; i < nums1Size; i++) {
ans[i] = map[nums1[i]];
if (ans[i] == 0) {
ans[i] = -1;
}
}
*returnSize = nums1Size;
return ans;
}