LeetCode - Daily Temperatures

Given a list of daily temperatures, produce a list 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 temperatures = [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].

竟然暴力解法也可AC:

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        if(temperatures == null || temperatures.length == 0){
            return null;
        }
        int len = temperatures.length;
        int[] res = new int[len];
        for(int i=0; i<len; i++){
            int count = 0;
            int cur = temperatures[i];
            for(int j = i+1; j<len; j++){
                int fu = temperatures[j];
                if(fu > cur){
                    count = j-i;
                    break;
                }
            }
            res[i] = count;
        }
        return res;
    }
}

  

原文地址:https://www.cnblogs.com/incrediblechangshuo/p/9691945.html

时间: 2024-10-03 15:14:01

LeetCode - Daily Temperatures的相关文章

[LeetCode] Daily Temperatures 日常温度

Given a list of daily temperatures, produce a list 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

739. Daily Temperatures - LeetCode

Question 739.?Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: public int[] dailyTemperatures(int[] temperatures) { int[] ans = new int[temperatures.length]; for (int i = 0; i<temperatures.length; i++) { int highId

LeetCode 739:每日温度 Daily Temperatures

题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]. Given a list of daily temperatures T, return a list such that, for each day in

[栈] leetcode 739 Daily Temperatures

problem:https://leetcode.com/problems/daily-temperatures/ 一道使用单调栈维护(递减序列)的题目,比较简单. class Solution { public: vector<int> dailyTemperatures(vector<int>& T) { stack<int> sta; int n = T.size(); vector<int> res(n, 0); for (int i = 0

739. Daily Temperatures

https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int> dailyTemperatures(vector<int>& temperatures) { stack<int> st; vector<int> res(temperatures.size()); for (int i = temperatures.si

LeetCode-739 Daily Temperatures Solution (with Java)

1. Description: 2.Solutions: 1 /** 2 * Created by sheepcore on 2019-05-07 3 */ 4 class Solution { 5 public int[] dailyTemperatures(int[] T) { 6 int[] res = new int[T.length]; 7 for(int i = 0; i < T.length -1; i++){ 8 boolean hasWarmerDay = false; 9 f

Leetcode daily 20/03/23 链表的中间结点

链表的中间结点 -题目- 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. -示例1- 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形式:[3,4,5]) 返回的结点值为 3 . (测评系统对该结点序列化表述是 [3,4,5]). 注意,我们返回了一个 ListNode 类型的对象 ans,这样: ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans

LeetCode 496. 下一个更大元素 I(Next Greater Element I) 35

496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组?nums1 和?nums2,其中 nums1?是?nums2?的子集.找到?nums1?中每个元素在?nums2?中的下一个比其大的值. nums1?中数字?x?的下一个更大元素是指?x?在?nums2?中对应位置的右边的第一个比?x?大的元素.如果不存在,对应位置输出 -1. 每日一算法2019/6/7Day 35LeetCode496. Next Greater Ele

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea