Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5560742

Browse files
EdgeAshazl397985856
authored andcommitted
feat: daily 2019年06月06日(daily-temperatures) (azl397985856#67)
* feat: longest harmonious subsequence * daily: Longest Harmonious Subsequence, option 1 * pref: make code universal. * pref: get better answer. * alternative solutions. * Update 2019年06月20日.md * Update 2019年06月20日.md * feat: date 2019年06月17日 * fix: add tag. * Update 2019年06月17日.md * Update 2019年06月17日.md * feat: 9.palindrome-number(2019年07月25日) * feat: daily 2019年06月06日(daily-temperatures) * pref: optimize code. * pref: one code for each solution.
1 parent 2bf04ed commit 5560742

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

‎daily/2019-06-06.md‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 毎日一题 - 739.Daily Temperatures
2+
3+
## 信息卡片
4+
5+
- 时间:2019年06月06日
6+
- 题目链接:https://leetcode.com/problems/daily-temperatures/
7+
- tag:`Array` `Stack`
8+
9+
## 题目描述
10+
11+
```
12+
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
13+
14+
For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].
15+
16+
Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].
17+
```
18+
19+
## 参考答案
20+
21+
暴力,双层for循环。`效率很低`
22+
23+
1. 外层是‘当天’T[i],内层是‘当天’之后T[j];
24+
2. 多少天之后比‘当天’温度高就是j-i;
25+
26+
时间复杂度O(n^2), 空间复杂度O(1)
27+
28+
参考JavaScript代码:
29+
30+
```js
31+
/**
32+
* @param {number[]} T
33+
* @return {number[]}
34+
* 双层for循环
35+
*/
36+
var dailyTemperatures = function(T) {
37+
let result = [];
38+
for(let i = 0; i < T.length; i++) {
39+
result[i] = 0;
40+
for(let j = i + 1; j < T.length; j++) {
41+
if (T[i] < T[j]) {
42+
result[i] = j - i;
43+
break;
44+
}
45+
}
46+
}
47+
return result;
48+
};
49+
```
50+
51+
使用栈,单调递减栈
52+
53+
1. for循环遍历数组,栈存T的下标i,返回结果数组result;
54+
2. 拿栈顶元素peek与i比较,T[peek] >= T[i]则将i入栈,T[peek] < T[i]则栈顶值(原数组下标)位置的天数就是result[peek] = i - peek;
55+
3. 栈顶元素出栈;
56+
4. 重复2,3两步;
57+
58+
时间复杂度O(n), 空间复杂度O(n)
59+
60+
参考JavaScript代码:
61+
62+
```js
63+
/**
64+
* @param {number[]} T
65+
* @return {number[]}
66+
* 递减栈;
67+
*/
68+
var dailyTemperatures = function(T) {
69+
let stack = [];
70+
let result = [];
71+
for (let i = 0; i < T.length; i++) {
72+
result[i] = 0;
73+
while(stack.length > 0 && T[stack[stack.length - 1]] < T[i]) {
74+
let peek = stack.pop();
75+
result[peek] = i - peek;
76+
}
77+
stack.push(i);
78+
}
79+
return result;
80+
};
81+
```
82+
83+
## 优秀解答
84+
85+
> 暂缺

‎daily/README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ tag: `Array`
2424

2525
时间: 2019年06月05日
2626

27+
#### [739.Daily Temperatures](./2019-06-06.md)
28+
29+
tag: `Array` `Stack`
30+
31+
时间: 2019年06月06日
32+
2733
#### [347.Top K Frequent Elements](./2019-06-08.md)
2834

2935
tag: `Hash Table` `Heap`

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /