Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

每日温度 #250

Open
louzhedong opened this issue Apr 28, 2021 · 0 comments
Open

每日温度 #250

louzhedong opened this issue Apr 28, 2021 · 0 comments

Comments

@louzhedong
Copy link
Owner

习题

请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。

例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。

提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。

思路

数组遍历

解答

javascript

/**
 * @param {number[]} T
 * @return {number[]}
 */
var dailyTemperatures = function(T) {
    var res = [], len = T.length;
    for (var i = 0; i < len; i++) {
        var wait = 0;
        for (var j = i + 1; j < len; j++) {
            if (T[j] > T[i]) {
                wait = j - i;
                break;
            }
        }
        res.push(wait);
    }
    return res;
};

go

func dailyTemperatures(T []int) []int {
    res := make([]int, 0)
    length := len(T)
    for i := 0; i < length; i++ {
        var wait int = 0
        for j := i + 1; j < length; j++ {
            if T[j] > T[i] {
                wait = j - i
                break
            }
        }
        res = append(res, wait)
    }
    return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant