My JavaScript simple solution


  • 0
    D
     * @param {number[]} findNums
     * @param {number[]} nums
     * @return {number[]}
     */
    var nextGreaterElement = function(findNums, nums) {
        
       for (var i = 0; i < findNums.length; i++) {
                    
            (function () {
                var isGreaterFround = false;
                var startSearchingNow = false;
    
                for (var j = 0; j < nums.length; j++) {
    
                    if (findNums[i] == nums[j]) {
                        startSearchingNow = true;
                    }
    
                    if (startSearchingNow && findNums[i] < nums[j]) {
                        findNums[i] = nums[j];
                        isGreaterFround = true;
                        break;
                    }
    
                }
    
                if (!isGreaterFround) {
                    findNums[i] = -1;
                }
    
            })();
            
        }
    
        return findNums;
        
    };
    

Log in to reply
 

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