• [LeetCode]739. Daily Temperatures


    Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

    For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

    Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].

    Solution

    这个题的题意很容易理解,就是按索引顺序找出每个元素比它自身大的元素离当前元素的距离。

    解法一

    老规矩,先给出一个最简单的解法。这个解法就是遍历数组,针对每个元素从该元素的索引位置开始遍历到数组末尾,直到遇到比自身大的元素的时候返回遍历的次数temp。可以说是很符合直觉的解法,性能当然也是意料之中的糟糕。

    class Solution {
        public int[] dailyTemperatures(int[] T) {
            int[] result=new int[T.length];
            for(int i=0;i<T.length;i++){
                int temp=0;
                for(int j=i+1;j<T.length;j++){
                      temp++;
                    if(T[j]>T[i]){
                        break;
                    }
                    if(j==T.length-1){
                        temp=0;
                    }
                }
                result[i]=temp;
            }
            return result;
        }
    }
    

    该解法性能如下:

    执行用时 : 653 ms, 在所有 Java 提交中击败了5.00%的用户
    内存消耗 :42.8 MB, 在所有 Java 提交中击败了93.00%的用户

  • 相关阅读:
    学习笔记 MYSQL报错注入(count()、rand()、group by)
    学习笔记 HTTP参数污染注入
    学习笔记 MSSQL显错手工注入
    代码审计入门后审计技巧
    字符串的排列
    二叉搜索树与双向链表
    复杂链表的复制
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列
    从上往下打印二叉树
  • 原文地址:https://www.cnblogs.com/rever/p/11614504.html
Copyright © 2020-2023  润新知