代码随想录打卡第58天|739. 每日温度;496.下一个更大元素 I

739. 每日温度

关键点1:先前的一些准备

单调栈:单调栈的含义->用栈记录已经遍历过的元素,再将0元素压入栈

结果集:与给定数组一样大小,结果集初始化为0

关键点2:核心部分

for循环遍历给定数组:有两种情况

情况1:if(temperatures[i] <= temperatures[st.peek()]);代表现在temperatures数组的元素值 <= temperatures[数组栈顶数字]的元素值,那此刻将i再压入栈中st.push(i);

情况2:!st.isEmpty() && temperatures[i] > temperatures[st.peek()]代表现在temperatures数组的元素值 > temperatures[栈顶数字]的元素值,那此刻则找到了大于栈顶数字位置对应数值的元素为 temperatures[i] ,对应的位置为i,

记录此时的结果answer[st.peek()] = i - st.peek();

结果记录后弹出栈内比i小的那个数

当结束while循环,则将此时 temperatures[i]的这个i再压入栈中st.push(i);

关键点3:结果

返回answer就行

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] answer = new int[temperatures.length];
        Stack<Integer> st = new Stack<>();
        st.push(0);
        for(int i = 1; i < temperatures.length;i++){
            if(temperatures[i] <= temperatures[st.peek()]){
                st.push(i);
            }else{
                while(!st.isEmpty() && temperatures[i] > temperatures[st.peek()]){
                    answer[st.peek()] = i - st.peek();
                    st.pop();
                }
                st.push(i);
            }
        }
        return answer;
    }
}

496.下一个更大元素 I  

关键点1:先前的一些准备

单调栈:单调栈的含义->用栈记录已经遍历过的元素,再将0元素压入栈

结果集:与nums1一样大小,结果集初始化为-1

HashMap:用map做nums1与nums2之间的映射,map的key为nums1中的元素,val为nums1中元素的位置

关键点2:核心部分

for循环遍历给定数组:有两种情况

情况1: if(nums2[i] <= nums2[st.peek()]);代表现在temperatures数组的元素值 <= temperatures[数组栈顶数字]的元素值,那此刻将i再压入栈中st.push(i);

情况2:!st.isEmpty()&& nums2[i] > nums2[st.peek()] 代表现在nums2数组的元素值 > nums2[栈顶数字]的元素值,那此刻则找到了大于栈顶数字位置对应数值nums2[st.peek()]的元素为 temperatures[i] ,对应的位置为i(注:用while的原因是判断是一个持续的过程)

关键点3:开始进入映射判断环节 

如果map中有这个元素nums2[st.peek()]

获取此时这个元素在map中的值,也就是这个元素对应的下标,Integer index = map.get(nums2[st.peek()]);

记录此时的结果answer[index] = nums2[i];  

结果记录后弹出栈内比i小的那个数

当结束while循环,则将此时 temperatures[i]的这个i再压入栈中st.push(i);

关键点3:结果

返回ans就行

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        // 用map做nums1与nums2之间的映射
        HashMap<Integer,Integer> map = new HashMap<>();
        // map的key为nums1中的元素,val为nums1中元素的位置
        for(int i = 0;i < nums1.length;i++){
            map.put(nums1[i],i);
        }
        // ans的长度与nums1有关,ans数组初始化为-1
        int[] ans = new int[nums1.length];        
        Arrays.fill(ans, -1);

        Stack<Integer> st = new Stack<>();
        st.push(0);
        for(int i = 1;i < nums2.length;i++){
            if(nums2[i] <= nums2[st.peek()]){
                st.push(i);
            }else{
                while(!st.isEmpty()&& nums2[i] > nums2[st.peek()]){
                    // if nums2[st.peek()]这个元素在map中出现过
                    if(map.containsKey(nums2[st.peek()])){                  
                        Integer index = map.get(nums2[st.peek()]);
                        ans[index] = nums2[i];                        
                    }
                    // 弹出这个需要放在if条件判断外面
                    st.pop();
                }
                st.push(i);
            }
        }
        return ans;   
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值