-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
[To Index](/index.md) | ||
--- | ||
#739. 每日温度 | ||
难度:Medium | ||
> 根据每日 气温 列表,请重新生成一个列表,对应位置的输出是需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高,请在该位置用 0 来代替。 | ||
例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。 | ||
|
||
提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。 | ||
|
||
|
||
从最后一天开始看,最后一天必定是0;然后前面的跟下一天比较,如果小于则为1,如果大于,则看下一天的结果是否为0,如果是0表示后面没有更高的温度,如果不为零,则和比下一天大的那一天开始比较,一直比较下去。 | ||
|
||
``` | ||
class Solution { | ||
public: | ||
vector<int> dailyTemperatures(vector<int>& T) { | ||
int left=T.size()-2; | ||
int right=left+1; | ||
vector<int>res(T.size(),0); | ||
if(T.size()<2) return res; | ||
while(left>=0) | ||
{ | ||
if(T[left]<T[left+1]) | ||
{ | ||
res[left]=1; | ||
} | ||
else | ||
{ | ||
int tmp=1; | ||
int ttmp=res[left+1]; | ||
while(ttmp) | ||
{ | ||
tmp+=ttmp; | ||
// cout<<tmp<<endl; | ||
if(T[left]<T[left+tmp]) | ||
{ | ||
res[left] =tmp; | ||
break; | ||
} | ||
ttmp=res[left+tmp]; | ||
} | ||
} | ||
left--; | ||
} | ||
return res; | ||
} | ||
}; | ||
``` | ||
|
||
> 执行用时 :108 ms, 在所有 C++ 提交中击败了77.64%的用户 | ||
内存消耗 :34.2 MB, 在所有 C++ 提交中击败了7.69%的用户 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters