739. 每日温度

题目描述:

给定一个整数数组 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]

提示:

  • 1 <= temperatures.length <= 105
  • 30 <= temperatures[i] <= 100

上代码,拿去即可运行

java 复制代码
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
        Integer[] temperatures = {73,74,75,71,69,72,76,73};
        Integer[] arr = getArray(temperatures);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "     ");
        }
    }

    public static Integer[] getArray(Integer[] array) {
        Integer[] arr = new Integer[array.length];
        Integer curr = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                curr++;
                if (array[i] < array[j]) {
                    arr[i] = curr;
                    curr = 0;
                    break;
                }

                if (j == array.length - 1) {
                    arr[i] = 0;
                    curr = 0;
                }

            }
        }
        arr[array.length - 1] = 0;
        return arr;
    }
}

运行结果:

我要刷300道算法题,第139道 。 希望自己可以坚持下去 。

相关推荐
浮灯Foden1 小时前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表
西工程小巴2 小时前
实践笔记-VSCode与IDE同步问题解决指南;程序总是进入中断服务程序。
c语言·算法·嵌入式
Tina学编程2 小时前
48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
java·算法
Moonbit2 小时前
MoonBit Perals Vol.06: MoonBit 与 LLVM 共舞 (上):编译前端实现
后端·算法·编程语言
百度Geek说3 小时前
第一!百度智能云领跑视觉大模型赛道
算法
big_eleven3 小时前
轻松掌握数据结构:二叉树
后端·算法·面试
big_eleven3 小时前
轻松掌握数据结构:二叉查找树
后端·算法·面试
CoovallyAIHub3 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·计算机视觉
kyle~5 小时前
OpenCV---特征检测算法(ORB,Oriented FAST and Rotated BRIEF)
人工智能·opencv·算法
初学小刘5 小时前
决策树:机器学习中的强大工具
算法·决策树·机器学习