【C/C++】每日温度 [ 栈的应用 ] 蓝桥杯/ACM备赛

数据结构考点:栈

题目描述:

给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。

输入输出描述:

|-----|--------------------------------------------|---------------------|
| | 输入 | 输出 |
| 示例1 | temperatures = [73,74,75,71,69,72,76,73] | [1,1,4,2,1,1,0,0] |
| 示例2 | temperatures = [30,40,50,60] | [1,1,1,0] |
| 示例3 | temperatures = [30,60,90] | [1,1,0] |

详细解答:

解答一:一种会超时的简单算法,不用"栈"

cpp 复制代码
int* dailyTemperatures(int* temperatures, int temperaturesSize, int* returnSize) {
    int *result,flag;
    result = (int*)malloc(sizeof(int) * temperaturesSize);
    returnSize = (int*)malloc(sizeof(int));
    *returnSize = temperaturesSize;
    for (int i = 0; i < temperaturesSize;i++)
    {
        flag = 0;
        int t = temperatures[i];
        for (int j = 1; j <= temperaturesSize-1-i;j++)
        {
            if(t<temperatures[i+j])
            {
                result[i] = j;
                flag = 1;
                break;
            }
        }
        if(flag==0)
            result[i] = 0;
    }
    return result;
}

解法二:利用"栈"

(由于题目来源于Leetcode,所以没有主函数)

该程序以C语言为主,利用了C++的一些函数:(但是不可以再Leetcode提交)

cpp 复制代码
#include<stdio.h>
#include<iostream>
#include<stack>
#include<stdc++.h>
using namespace std;
int* dailyTemperatures(int* temperatures, int temperaturesSize, int* returnSize) {
    int *result;
    result = (int*)malloc(sizeof(int) * temperaturesSize);
    memset(result, 0, sizeof(int) * temperaturesSize);
    *returnSize = temperaturesSize;
    stack<int> sta;
    sta.push(0);
    int i, j;
    for (i = 1;i<temperaturesSize;i++)
    {
        if(temperatures[sta.top()]<temperatures[i])
        {
            while(!sta.empty()&&temperatures[sta.top()]<temperatures[i])
            {
                result[sta.top()] = i - sta.top();
                sta.pop();
            }
            sta.push(i);
        }
        else 
        {
            sta.push(i);
        }
    }
    return result;
}

解法三:C++

cpp 复制代码
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {

    vector<int> result(temperatures.size(), 0);
    stack<int> sta;
    sta.push(0);
    int i, j;
    for (i = 1;i<temperatures.size();i++)
    {
        if(temperatures[sta.top()]<temperatures[i])
        {
            while(!sta.empty()&&temperatures[sta.top()]<temperatures[i])
            {
                result[sta.top()] = i - sta.top();
                sta.pop();
            }
            sta.push(i);
        }
        else 
        {
            sta.push(i);
        }
    }
    return result;

    }
};

题目来源:Leetcode739.【 739. 每日温度 - 力扣(LeetCode)

相关推荐
Han.miracle8 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
北冥湖畔的燕雀9 小时前
C++泛型编程(函数模板以及类模板)
开发语言·c++
mit6.82410 小时前
前后缀分解
算法
QX_hao11 小时前
【Go】--map和struct数据类型
开发语言·后端·golang
独自破碎E11 小时前
判断链表是否为回文
数据结构·链表
你好,我叫C小白11 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
Evand J12 小时前
【MATLAB例程】基于USBL和DVL的线性回归误差补偿,对USBL和DVL导航数据进行相互补偿,提高定位精度,附代码下载链接
开发语言·matlab·线性回归·水下定位·usbl·dvl
朱嘉鼎13 小时前
状态机的介绍
c语言·单片机
Larry_Yanan13 小时前
QML学习笔记(四十二)QML的MessageDialog
c++·笔记·qt·学习·ui
寂静山林13 小时前
UVa 10228 A Star not a Tree?
算法