Next Greater Element I

来源:互联网 发布:淘宝 毛毯垫 编辑:程序博客网 时间:2024/05/16 18:50

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.

var nextGreaterElement = function(findNums, nums) {    var arr = [];    for (var i = 0; i < findNums.length; i++) {      arr[i] = -1;      for (var j = nums.indexOf(findNums[i]) + 1; j < nums.length; j++) {        if (nums[j] > findNums[i]) {          arr[i] = nums[j];          break        }      }    }    return arr};
0 0