Concise C solution


  • 0
    int* nextGreaterElement(int* findNums, int findNumsSize, int* nums, int numsSize, int* returnSize) {
        int *greaters = malloc(sizeof(int) * findNumsSize);
        
        for(int i=0; i<findNumsSize; ++i){
            int cur = findNums[i];
            int greaterNum = -1;
            int j=0;
            for(; j<numsSize; ++j){
                if(nums[j] == cur)
                    break;
            }
            for(; j<numsSize; ++j){
                if(nums[j]>cur){
                    greaterNum = nums[j];
                    break;
                }
            }
            greaters[i] = greaterNum;
        }
        
        *returnSize = findNumsSize;
        return greaters;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.