leetcode 496 Next Greater Element I

来源:互联网 发布:新闻资讯网站源码 编辑:程序博客网 时间:2024/06/07 09:14

原题:

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.


The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

题目的意思

就是找到对应元素后面比他大的元素。

int* nextGreaterElement(int* findNums, int findNumsSize, int* nums, int numsSize, int* returnSize) {    int* result;    result=(int *)malloc(sizeof(int)*findNumsSize);    *returnSize=findNumsSize;    for(int n=0;n<findNumsSize;n++)    {        int flag=0;        *(result+n)=-1;        for(int m=0;m<numsSize;m++)        {            if(*(findNums+n)==*(nums+m))            {                flag=1;            }            if(flag==1&&*(nums+m)>*(findNums+n))            {                *(result+n)=*(nums+m);                break;            }        }    }    return result;}
就是简单的扫描。

原创粉丝点击