739. 每日温度
力扣题目链接
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
Deque<Integer> stack = new LinkedList<>();
int len =temperatures.length;
int[] result = new int[len];
stack.push(0);
for(int i = 1 ;i < len;i++){
if(temperatures[i] <= temperatures[stack.peek()]){
stack.push(i);
}else{
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
result[stack.peek()] = i - stack.peek();
stack.pop();
}
stack.push(i);
}
}
return result;
}
}
496.下一个更大元素 I
力扣题目链接
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
//从二里面找出1对应的下标,排序一下,在遍历2
int[] result = new int[nums1.length];
Map<Integer,Integer> map = new HashMap();
Deque<Integer> stack = new LinkedList<>();
for(int i = 0;i < nums1.length;i++){
map.put(nums1[i],i);
}
Arrays.fill(result,-1);
stack.push(0);
for(int i = 1;i < nums2.length;i++){
if(nums2[i] <= nums2[stack.peek()]){
stack.push(i);
}else{
while(!stack.isEmpty() && nums2[i] > nums2[stack.peek()]){
if(map.containsKey(nums2[stack.peek()])){
result[map.get(nums2[stack.peek()])] = nums2[i];
}
stack.pop();
}
stack.push(i);
}
}
return result;
}
}