LeetCode [栈] 496.Next Greater Element I(C++和Python实现)

本博客介绍了LeetCode的496题——Next Greater Element I,这是一个简单的哈希表和栈问题。题目要求找出数组nums1中的元素在数组nums2中的下一个更大元素,如果不存在则返回-1。文章提供了C++和Python两种解法,C++解法首先尝试暴力解法,然后使用栈进行优化。Python解法同样基于哈希表和栈的思想。

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

496 Next Greater Element I [难度:简单] [哈希表/栈]

【题目】

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.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
    For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
    For number 1 in the first array, the next greater number for it in the second array is 3.
    For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
    For number 2 in the first array, the next greater number for it in the second array is 3.
    For number 4 in the first array, there is no next greater number for it in the second array, so output -1.

Note:

  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000. 

【题解C++】

先上个暴力解法吧~思路大概是这样:首先看清题意,不是同一位置右边的第一个最大值,而是同一个值右边的最大值!既然是靠值来联系两个数组,那第一个就会想到哈希表啦。先遍历大的数组,找到每个值对应的右边第一个最大值,将二者一个作为键一个作为值。最后再遍历第一个数组,push_back。

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        int i,j,hash[100000];
        vector<int> out;
        //建立映射表
        for(i=0;i<nums2.size();i++)
        {
            //当前值
            int tmp = nums2[i];
            for(j=i+1;j<nums2.size();j++)
            {    
                //如果右边有比当前值大的,存入哈希表
                if(nums2[j]>tmp)
                {
                    hash[tmp] = nums2[j];
                    break;
                }          
            }
            //否则将-1存入哈希表
            if(j==nums2.size())
                hash[tmp] = -1;
                
        }
        for(i=0;i<nums1.size();i++)
            out.push_back(hash[nums1[i]]);
        return out;
    }
};

接下来优化一下~既然是栈的合集,那就用栈来实现8!

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int> out;
        stack<int> st;
        map<int,int> mp;
        //倒着遍历大数组
        for(int i=nums2.size()-1;i>=0;i--)
        {
            //当前数组值
            int now = nums2[i];
            //只要栈不为空&&栈顶值不大于当前值,pop以寻找更大的值
            while(!st.empty()&&st.top()<=now)
                st.pop();
            //如果栈为空,说明没找到比当前值更大的,赋值为-1,否则赋值为栈顶值
            mp[now] = st.empty()?-1:st.top();
            //入栈
            st.push(now);
        }
        for(int i=0;i<nums1.size();i++)
            out.push_back(mp[nums1[i]]);
        return out;
    }
};

【解题Python】

Python用的暴力,思路和C++也是一样的。

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        st = []
        for i in nums1:
            index = nums2.index(i)
            flag = 0
            for j in nums2[index:]:
                if j>i:
                    st.append(j)
                    flag = 1
                    break
            if not flag:
                st.append(-1)
        return st
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值