496. Next Greater Element I

来源:互联网 发布:桌面上软件打不开 编辑:程序博客网 时间:2024/05/17 23:16

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.

这个除了一个个比还有其它方法?

/** * @param {number[]} findNums * @param {number[]} nums * @return {number[]} */var nextGreaterElement = function(findNums, nums) {    var result=[];    for(var i=0;i<findNums.length;i++){        result[i]=-1;        for(var j=nums.indexOf(findNums[i])+1;j<nums.length;j++){            if(findNums[i]<nums[j]){                result[i]=nums[j];                break;            }        }    }    return result;};
0 0
原创粉丝点击