[JavaScript] with easily logical to solve it


  • 0
    S
    var nextGreaterElements = function(nums) {
    var len = nums.length;
    var i,j,temp,flag;
    var result = [];
    
    Outer:
        for(i=0;i<len;i++){
        	temp = i+1;
        	result[i] = -1;
        	flag = true;
        	for(j = temp;j<len;j++){
        		if(nums[j]>nums[i]){
        			result[i] = nums[j];
        			continue Outer;
        		}
        	}
        	if(j===len&&flag===true&&i!==0){
    			flag = false;
    			for(j = 0;j<i;j++){
    				if(nums[j]>nums[i]){
    	    			result[i] = nums[j];
    	    			continue Outer;
    	    		}
    			}
    		}
        }
    	return result;
    

    };


Log in to reply
 

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