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

ops/2025/2/10 9:30:39/

数据结构考点:栈

题目描述:

给定一个整数数组 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/ops/157205.html

相关文章

Visual Studio(VS)初始配置环境(scanf异常)

发现问题 当我们第一次安装Visual Studio&#xff08;VS&#xff09;且没有初次环境配置时&#xff0c;用某些函数时会发现报错异常。&#xff08;如下scanf函数为例&#xff09; #include<stdio.h>int main() {int a 0;scanf("%d", &a);printf("%…

【北上广深杭大厂编程面试题】C++篇...这里介绍堆区和栈区的区别?(二)

【北上广深杭大厂编程面试题】C篇…这里介绍堆区和栈区的区别&#xff1f;&#xff08;二&#xff09; 【北上广深杭大厂编程面试题】C篇…这里介绍堆区和栈区的区别&#xff1f;&#xff08;二&#xff09; 文章目录 【北上广深杭大厂编程面试题】C篇...这里介绍堆区和栈区的…

DeepSeek使用技巧大全(含本地部署教程)

在人工智能技术日新月异的今天&#xff0c;DeepSeek 作为一款极具创新性和实用性的 AI&#xff0c;在众多同类产品中崭露头角&#xff0c;凭借其卓越的性能和丰富的功能&#xff0c;吸引了大量用户的关注。 DeepSeek 是一款由国内顶尖团队研发的人工智能&#xff0c;它基于先进…

51单片机独立按键的扩展应用

提示&#xff1a; 按键S7和S6为选择键&#xff0c;确定控制键控制那组LED指示灯。按键S5和S4为控制键&#xff0c;按键该键点亮指定的LED指示灯&#xff0c;松开后熄灭。按下S7点亮L1指示灯&#xff0c;L1点亮后&#xff0c;S6不响应操作&#xff0c;S5控制L3&#xff0c;S4控…

Go 中的 7 个常见接口错误

Go 仍然是一门新语言,如果你正在使用它,它很可能不是你的第一门编程语言。 不同的语言,既为你带来了经验,也带来了偏见。你用以前的任何语言做的事情,在 Go 中用相同的方法可能不是一个好主意。 学习 Go 不仅仅是学习一种新的语法。这也是学习一种新的思维方式来思考你的…

postgresql 游标(cursor)的使用

概述 PostgreSQL游标可以封装查询并对其中每一行记录进行单独处理。当我们想对大量结果集进行分批处理时可以使用游标&#xff0c;因为一次性处理可能造成内存溢出。 另外我们可以定义函数返回游标类型变量&#xff0c;这是函数返回大数据集的有效方式&#xff0c;函数调用者…

未来AI医院蓝图:源码、机器人与数字孪生如何打造智能医疗APP?

在人工智能&#xff08;AI&#xff09;、物联网&#xff08;IoT&#xff09;和大数据技术的推动下&#xff0c;医疗行业正在经历一场深刻的变革。从传统医院到互联网医院&#xff0c;再到智能医疗生态的构建&#xff0c;未来的AI医院不仅能提供更高效的医疗服务&#xff0c;还能…

Kotlin 循环与函数详解:高效编程指南

Kotlin 循环 当您处理数组时&#xff0c;经常需要遍历所有元素。 要遍历数组元素&#xff0c;请使用 for 循环和 in 操作符&#xff1a; 示例 输出 cars 数组中的所有元素&#xff1a; val cars arrayOf("Volvo", "BMW", "Ford", "Maz…