当前位置:网站首页>【Hot100】739. Daily temperature
【Hot100】739. Daily temperature
2022-07-06 06:51:00 【Wang Liuliu's it daily】
739. Daily temperature
Given an array of integers temperatures , Indicates the daily temperature , Returns an array answer , among answer[i] Refers to the i God , The next higher temperature appears in a few days . If the temperature doesn't rise after that , Please use... In this position 0 Instead of .
Input : temperatures = [73,74,75,71,69,72,76,73]
Output : [1,1,4,2,1,1,0,0]
Topic understanding :
For input 73, It needs to After a day Until the temperature rises , That is, the next day , The temperature rises to 74 , So the corresponding result is 1.
For input 74, It needs to the One day Until the temperature rises , On the third day , The temperature rises to 75 , So the corresponding result is 1.
For input 75, It passes by 1 The temperature was found to be 71, No more than it , Keep waiting , always Wait four days , Wait until the temperature rises on the seventh day , The temperature rises to 76 , So the corresponding result is 4 .
For input 71, It passes by 1 The temperature was found to be 69, No more than it , Keep waiting , always Waited two days. , Wait until the temperature rises on the sixth day , The temperature rises to 72 , So the corresponding result is 2 .
For input 69, it After a day It turns out that the temperature is 72, It's over it , So the corresponding result is 1 .
For input 72, it After a day It turns out that the temperature is 76, It's over it , So the corresponding result is 1 .
For input 76, follow-up There is no temperature Can surpass it , So the corresponding result is 0 .
For input 73, follow-up There is no temperature Can surpass it , So the corresponding result is 0 .
idea : For each temperature value Search backward in turn , Find a value higher than the current temperature , This is the easiest way to think about it .
principle : From left to right, all numbers except the last one are traversed once , The result of the last data must be 0, There is no need to calculate .
When traversing , Every number goes backward , Until we find a larger number , Count frequency Is the corresponding output value .
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int len = temperatures.length;
int[] res = new int[len];
for(int i=0;i<len;i++){
int cur = temperatures[i];
if(cur < 100){
for(int j=i+1;j<len;j++){
if(temperatures[j] > cur){
res[i] = j-i;
break;
}
}
}
}
return res;
}
}
Use the stack to solve :
Decrement stack : There are only diminishing elements in the stack .
Traverse the entire array , If the stack is not empty , And the current number is greater than the stack top element , So if you go directly to the stack, it's not Decrement stack , So you need to take out the top element of the stack , Because the current number is larger than the number of the top element of the stack , And it must be the first one greater than the number of elements at the top of the stack , Directly find out the subscript difference is the distance between the two .
Keep looking at the new stack top elements , Until the current number is less than or equal to the top element of the stack , Then put the numbers on the stack , This keeps the decrement stack going , And the distance between each number and the first number greater than it can also be calculated .
class Solution {
public int[] dailyTemperatures(int[] T) {
Stack<Integer> stack = new Stack<>();
int length = T.length;
int[] result = new int[length];
for (int i = 0; i < length; i++) {
while (!stack.isEmpty() && T[i] > T[stack.peek()]) {
int pre = stack.pop();
result[pre] = i - pre;
}
stack.add(i);
}
return result;
}
}
边栏推荐
- Redis Foundation
- [ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
- Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
- [advanced software testing step 1] basic knowledge of automated testing
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
- 《从0到1:CTFer成长之路》书籍配套题目(周更)
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- [unity] how to export FBX in untiy
- Leetcode - 152 product maximum subarray
猜你喜欢
医疗软件检测机构怎么找,一航软件测评是专家
Introduction and underlying analysis of regular expressions
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
顶测分享:想转行,这些问题一定要考虑清楚!
Wish Dragon Boat Festival is happy
ECS accessKey key disclosure and utilization
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Thesis abstract translation, multilingual pure human translation
My creation anniversary
随机推荐
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
LeetCode - 152 乘积最大子数组
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
编译,连接 -- 笔记 -2
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
AI on the cloud makes earth science research easier
Leetcode daily question (1997. first day where you have been in all the rooms)
[brush questions] how can we correctly meet the interview?
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Automated test environment configuration
It is necessary to understand these characteristics in translating subtitles of film and television dramas
攻防世界 MISC中reverseMe简述
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Today's summer solstice
[ 英语 ] 语法重塑 之 英语学习的核心框架 —— 英语兔学习笔记(1)
详解SQL中Groupings Sets 语句的功能和底层实现逻辑