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

devtools/2025/2/12 4:48:41/

数据结构考点:栈

题目描述:

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

输入输出描述:

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

详细解答:

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

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提交)

#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++

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)】


http://www.ppmy.cn/devtools/158115.html

相关文章

Scala语言的系统运维

Scala语言的系统运维 引言 在今天的科技发展时代&#xff0c;软件系统的复杂性和规模不断增加&#xff0c;因此系统运维的管理和监控显得尤为重要。在众多编程语言中&#xff0c;Scala因其高度的表达力和强大的性能而受到越来越多开发者和运维人员的青睐。本文将探讨Scala语言…

Android车机DIY开发之软件篇(十) NXP MfgTool和UUU的使用

标题Android车机DIY开发之软件篇(十) NXP MfgTool和UUU的使用 一、MfgTool工具 1.基本原理 1、先向DDR下载一个linux系统2. 通过linux完成烧写files里面保存的是最终保存到开发板中的uboot.imx zimage dtb rootfsvbs是在打开mfgtool2和很多参数ucl2.xml表示文件选择 定义自…

【Spring Boot】SpringBoot自动装配-Import

目录 一、前言二、 定义三、使用说明 3.1 创建项目 3.1.1 导入依赖3.1.2 创建User类 3.2 测试导入Bean 3.2.1 修改启动类 3.3 测试导入配置类 3.3.1 创建UserConfig类3.3.2 修改启动类 3.4 测试导入ImportSelector 3.4.1 创建UseImportSelector类3.4.2 修改启动类3.4.3 启动测试…

PostgreSQL插件-pg_stat_statements-安装和使用

文章目录 插件介绍插件安装1.修改配置文件postgresql.conf2.插件相关参数参数默认值参数说明特别注意pg_stat_statements.max参数设置太小日志会有警告 插件使用1.创建插件2.使用插件3.重置数据4.删除插件 可能会出现的问题1.没有编译安装插件2.没有配置shared_preload_librari…

使用WebUI访问本地Deepseek(Ollama集成Open WebUI)

在《deepseek本地部署和使用&#xff08;Linux虚拟机&#xff09;》中&#xff0c;我们使用Ollama部署了Deepseek-r1&#xff0c;但是只能通过命令行方式交互&#xff0c;默认Ollama启动后&#xff0c;会启动一个监听到127.0.0.1&#xff0c;用以接收POST 请求&#xff0c;服务…

仿 RabbitMQ 实现的简易消息队列

文章目录 项目介绍开放环境第三⽅库介绍ProtobufMuduo库 需求分析核⼼概念实现内容 消息队列系统整体框架服务端模块数据管理模块虚拟机数据管理模块交换路由模块消费者管理模块信道&#xff08;通信通道&#xff09;管理模块连接管理模块 客户端模块 公共模块日志类其他工具类…

Java GSON 解析 JSON 完全指南

1. 简介 GSON&#xff08;Google JSON&#xff09;是 Google 提供的用于在 Java 中处理 JSON 数据的库。它允许 Java 对象与 JSON 之间进行序列化和反序列化&#xff0c;支持简单对象、集合、泛型和复杂数据结构的转换。GSON 轻量、高效、易用&#xff0c;是 Java 开发中处理 …

Java面试题-计算机网络

文章目录 1.介绍一下TCP/IP五层模型&#xff1f;2.**什么是TCP三次握手、四次挥手&#xff1f;**1.三次握手建立连接2.四次握手断开连接 **3.HTTPS和HTTP的区别是什么&#xff1f;**4.**浏览器输入www.taobao.com回车之后发生了什么**&#xff1f;1.URL解析&#xff0c;对URL进…