当前位置: 代码迷 >> 综合 >> Leetcode 739. Daily Temperatures
  详细解决方案

Leetcode 739. Daily Temperatures

热度:94   发布时间:2023-12-12 21:21:04.0

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Daily Temperatures

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

  1. https://leetcode.com/problems/daily-temperatures/description/