LeetCode 496.下个更大的数

本文介绍了如何解决LeetCode上的第496题,即找到两个无重复元素数组中,第一个数组每个元素在第二个数组中的下一个更大元素。提供了两种解法:暴力破解法和单调栈法,并给出了相应的Java实现代码。通过这种方法,可以有效地在数组中查找目标元素的后续更大值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode 496. 下一个更大元素 I

给你两个 没有重复元素 的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。

请你找出 nums1 中每个元素在 nums2 中的下一个比其大的值。

nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出 -1 。

示例:

输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
输出: [-1,3,-1]
解释:
    对于 num1 中的数字 4 ,你无法在第二个数组中找到下一个更大的数字,因此输出 -1 。
    对于 num1 中的数字 1 ,第二个数组中数字1右边的下一个较大数字是 3 。
    对于 num1 中的数字 2 ,第二个数组中没有下一个更大的数字,因此输出 -1 。

解题:

方法一:暴力破解法,遍历两个数组,在第二个数组中找到第一个数组每个元素的位置,再遍历第二个数组剩下的元素作比较,有更大的值则赋值给第一个数组。

代码1:

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int b;
        int a;
        for(a = 0; a < nums1.length; a++){
            for(b = 0; b < nums2.length; b++){
                if(nums1[a] == nums2[b]){
                    while(b < nums2.length){
                        b++;
                        try{
                            if(nums1[a] < nums2[b]){
                            nums1[a] = nums2[b];
                            break;
                            }
                        }catch(Exception e){
                             nums1[a] = -1;
                        }
                    }
                }
            }
        }
       return nums1;
    }
}

代码2(官方):


public class Solution {

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;

        int[] res = new int[len1];
        if (len1 < 1) {
            return res;
        }

        for (int i = 0; i < len1; i++) {
            int curVal = nums1[i];
            int j = 0;
            while (j < len2 && nums2[j] != curVal) {
                j++;
            }

            // 此时 nums[j] = nums[i]
            j++;
            while (j < len2 && nums2[j] < curVal) {
                j++;
            }

            if (j == len2) {
                res[i] = -1;
                continue;
            }
            res[i] = nums2[j];
        }
        return res;
    }
}

方法二:单调栈,利用栈找出nums2数组中每个元素的对应的下一个更大的数存入哈希表,在遍历nums1数组,从哈希表找出对应的值。

代码:

public class Solution {

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;

        Deque<Integer> stack = new ArrayDeque<>();
        Map<Integer, Integer> map = new HashMap<>();
        // 先处理 nums2,把对应关系存入哈希表
        for (int i = 0; i < len2; i++) {
            while (!stack.isEmpty() && stack.peekLast() < nums2[i]) {
                //stack.removeLast()移除并返回最后一个元素
                map.put(stack.removeLast(), nums2[i]);
            }
            stack.addLast(nums2[i]);
        }
        // 遍历 nums1 得到结果集
        int[] res = new int[len1];
        for (int i = 0; i < len1; i++) {
            //Map.getOrDefault(Object key, V defaultValue)方法的作用是:
  			//当Map集合中有这个key时,就使用这个key值;
  			//如果没有就使用默认值defaultValue。
            res[i] = map.getOrDefault(nums1[i], -1);
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值