广州做网站哪个平台好,高端品牌网站建设(杭州),现代化专业群建设网站,外贸收款平台有哪些题目#xff1a;
给定一个整数数组 temperatures #xff0c;表示每天的温度#xff0c;返回一个数组 answer #xff0c;其中 answer[i] 是指对于第 i 天#xff0c;下一个更高温度出现在几天后。如果气温在这之后都不会升高#xff0c;请在该位置用 0 来代替。
方法
给定一个整数数组 temperatures 表示每天的温度返回一个数组 answer 其中 answer[i] 是指对于第 i 天下一个更高温度出现在几天后。如果气温在这之后都不会升高请在该位置用 0 来代替。
方法灵神从右往左遍历当前元素若大于等于栈顶元素要出栈直至当前元素小于栈顶元素然后记录ans[ i ]并把当前元素入栈。
代码
class Solution {public int[] dailyTemperatures(int[] temperatures) {int n temperatures.length;int[] ans new int[n];DequeInteger st new LinkedList();// 从右到左for (int i n - 1; i 0; i--) {int t temperatures[i];while (!st.isEmpty() t temperatures[st.peek()]) {st.pop();}if (!st.isEmpty())ans[i] st.peek() - i;st.push(i);}return ans;}
} 题目规定 30 temperatures[i] 100