文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
- Version 1
class Solution {
public:vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<int> result;for(int i = 0; i < temperatures.size(); i++) {
bool flag = false;for(int j = i + 1; j < temperatures.size(); j++) {
if(temperatures[j] > temperatures[i]) {
flag = true;result.push_back(j - i);break;}}if(!flag) {
result.push_back(0);}}return result;}
};
- Version 2
class Solution {
public:vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<int> result(temperatures.size(), 0);stack<int> s;for(int i = 0; i < temperatures.size(); i++) {
while(!s.empty() && temperatures[s.top()] < temperatures[i]) {
result[s.top()] = i - s.top();s.pop();}s.push(i);}return result;}
};
Reference
- https://leetcode.com/problems/daily-temperatures/description/