• 496. 下一个更大元素 I(javascript)496. Next Greater Element I


    nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。

    给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。

    对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。

    返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。

    The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.

    You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.

    For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.

    Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.

    示例 1:

    输入:nums1 = [4,1,2], nums2 = [1,3,4,2].
    输出:[-1,3,-1]
    解释:nums1 中每个值的下一个更大元素如下所述:
    - 4 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。
    - 1 ,用加粗斜体标识,nums2 = [1,3,4,2]。下一个更大元素是 3 。
    - 2 ,用加粗斜体标识,nums2 = [1,3,4,2]。不存在下一个更大元素,所以答案是 -1 。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    示例 2:

    输入:nums1 = [2,4], nums2 = [1,2,3,4].
    输出:[3,-1]
    解释:nums1 中每个值的下一个更大元素如下所述:
    - 2 ,用加粗斜体标识,nums2 = [1,2,3,4]。下一个更大元素是 3 。
    - 4 ,用加粗斜体标识,nums2 = [1,2,3,4]。不存在下一个更大元素,所以答案是 -1 。
    
    • 1
    • 2
    • 3
    • 4
    • 5

    提示:

    1 <= nums1.length <= nums2.length <= 1000
    0 <= nums1[i], nums2[i] <= 104
    nums1和nums2中所有整数 互不相同
    nums1 中的所有整数同样出现在 nums2 中
    
    • 1
    • 2
    • 3
    • 4

    根据题意:

    1. 对nums1进行遍历,每个元素找到在nums2中的下标;let j = nums2.indexOf(nums1[i])
    2. 对nums2进行循环,循环范围[j,len2),当发现第一个比nums1[i]大,取最大值,跳出循环;如果没有发现进行j++;
    3. 对于max = -1,当通过循环找到更大值,将会替换nums1[i];没有找到,返回-1
    4. 因为每个循环过后,nums1[i]元素将不会使用,所以为了优化代码,直接对nums1[i]进行修改
    var nextGreaterElement = function (nums1, nums2) {
    	let len1 = nums1.length
        let len2 = nums2.length
        for (let i = 0; i < len1; i++) {
            let j = nums2.indexOf(nums1[i])
            let max = -1
            while (j < len2) {
                if (nums2[j] > nums1[i]) {
                    max = Math.max(max, nums2[j])
                    j = len2
                }
                j++
            }
            nums1[i] = max
        }
        return nums1
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    leetcode:https://leetcode.cn/problems/next-greater-element-i/

  • 相关阅读:
    数据存储选项
    浅谈中国汽车充电桩行业市场状况及充电桩选型的介绍
    vite项目运行后只显示主机地址
    【网络篇】第十五篇——HTTP协议(二)
    软件设计师中级
    MATLAB点云处理(二十六):将无序点云转换为有序点云(pcorganize),删除无效点(removeInvalidPoints)
    安装pandas报错
    .net core webapi 添加日志管理看板LogDashboard
    React入门笔记(一)
    java动态代理-面向对象的补充
  • 原文地址:https://blog.csdn.net/ingenuou_/article/details/126729567