🎈LeetCode739. 每日温度
链接:739.每日温度
给定一个整数数组
temperatures,表示每天的温度,返回一个数组answer,其中answer[i]是指对于第i天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用0来代替。

public int[] dailyTemperatures(int[] temperatures) {
// 单调栈
int[] result=new int[temperatures.length];
Stack<Integer> st=new Stack<>();
for(int i=0;i<temperatures.length;i++){
if(!st.isEmpty()){
while(!st.isEmpty() && temperatures[st.peek()]<temperatures[i]){
result[st.peek()]=i-st.peek();
st.pop();
}
st.push(i);
}else{
st.push(i);
}
}
return result;
}
🎈LeetCode 496.下一个更大元素 I
链接:496.下一个更大元素
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]是如上所述的 下一个更大元素 。

public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] result=new int[nums1.length];
Arrays.fill(result,-1);
Stack<Integer> st=new Stack<>();
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums1.length;i++){
map.put(nums1[i],i);
}
st.add(0);
for(int i=1;i<nums2.length;i++){
if(nums2[i]<=nums2[st.peek()]){
st.add(i);
}else{
while(!st.isEmpty() && nums2[i]>nums2[st.peek()]){
if (map.containsKey(nums2[st.peek()])){
Integer index = map.get(nums2[st.peek()]);
result[index] = nums2[i];
}
st.pop();
}
st.add(i);
}
}
return result;
}











![[模拟电路]集成运算放大器](https://img-blog.csdnimg.cn/28cc38a4d71c44318fe8068c2010fc80.png)







