739. 每日温度
为保证权益,题目请参考 739. 每日温度(From LeetCode).
解决方案1
CPP
C++
//
// Created by lenovo on 2020/6/11.
//
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <climits>
#include <stack>
using namespace std;
class Solution {
public:
vector<int> dailyTemperatures(vector<int> &T) {
vector<int> res(T.size());
int maps[101];
fill(maps, maps + 101, INT_MAX);
stack<int> stacks;
stacks.push(INT_MAX);
for (int i = T.size() - 1; i >= 0; --i) {
// stack
if (T[i] >= stacks.top()) {
stacks.pop();
while (T[i] >= stacks.top()) {
stacks.pop();
}
}
// map
maps[T[i]] = i;
if (stacks.top() == INT_MAX) {
res[i] = 0;
} else {
res[i] = maps[stacks.top()] - maps[T[i]];
}
stacks.push(T[i]);
}
return res;
}
};
int main() {
vector<int> vec;
vec.push_back(73);
vec.push_back(74);
vec.push_back(75);
vec.push_back(71);
vec.push_back(69);
vec.push_back(72);
vec.push_back(76);
vec.push_back(73);
Solution so;
for (auto x: so.dailyTemperatures(vec)) {
cout << x << endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64