LC 503 JS

pimkle發表於2018-08-21

backup original slow but accepted

code

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var nextGreaterElements = function(nums) {
    let len = nums.length;
    let res = [];
    for(let i = 0; i < len; ++i){
        let ans = -1;
        for(let j = (i + 1) % len; j !== i; j = ++j % len){
            if(nums[j] > nums[i]){
                ans = nums[j];
                break;
            }
        }
        res.push(ans);
    }
    return res;

複製程式碼

相關文章