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 highIdx = i;
        for (int j=i+1; j<temperatures.length; j++) {
            if (temperatures[j] > temperatures[i]) {
                highIdx = j;
                break;
            }
        }
        ans[i] = highIdx - i;
    }
    return ans;
}

Ref

别人实现高效的方法

https://leetcode.com/problems/daily-temperatures/discuss/109832/Java-Easy-AC-Solution-with-Stack

Stack

public int[] dailyTemperatures(int[] temperatures) {
    Stack<Integer> stack = new Stack<>();
    int[] ret = new int[temperatures.length];
    for(int i = 0; i < temperatures.length; i++) {
        while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
            int idx = stack.pop();
            ret[idx] = i - idx;
        }
        stack.push(i);
    }
    return ret;
}

Array

public int[] dailyTemperatures(int[] temperatures) {
    int[] stack = new int[temperatures.length];
    int top = -1;
    int[] ret = new int[temperatures.length];
    for(int i = 0; i < temperatures.length; i++) {
        while(top > -1 && temperatures[i] > temperatures[stack[top]]) {
            int idx = stack[top--];
            ret[idx] = i - idx;
        }
        stack[++top] = i;
    }
    return ret;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9388225.html

时间: 2024-11-09 10:42:40

739. Daily Temperatures - LeetCode的相关文章

[栈] 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

题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 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] 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

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

[Lintcode]739. 24 Game/[Leetcode]679. 24 Game

?[Lintcode]739. 24 Game/[Leetcode]679. 24 Game 本题难度: Hard/Medium Description You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through?*,?/,?+,?-,?(,?)?to get the value of 24. Example 1: Input: [4, 1

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

arts-week2

Algorithm 739. Daily Temperatures - LeetCode 535. Encode and Decode TinyURL - LeetCode 811. Subdomain Visit Count - LeetCode 706. Design HashMap - LeetCode 771. Jewels and Stones - LeetCode 204. Count Primes - LeetCode 445. Add Two Numbers II - LeetC

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