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;
}
}
另一种使用stack的解法:
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
if(temperatures == null || temperatures.length == 0){
return null;
}
int len = temperatures.length;
int[] res = new int[len];
Stack<Integer> stack = new Stack<>();
for(int i = 0; i<len; i++){
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
int temp = stack.pop();
res[temp] = i - temp;
}
stack.push(i); }
return res;
}
}
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 ma ...
- [Leetcode 739]*还有几天会升温 Daily Temperatures
[题目] Given a list of daily temperatures T, return a list such that, for each day in the input, tells ...
- LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
- 739. Daily Temperatures - LeetCode
Question 739. Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: p ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- [Swift]LeetCode739. 每日温度 | Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
- Leetcode739 - Daily Temperatures
题目描述 Leetcode 739 本题考察了栈的使用.题目输入是一段温度值列表,然后返回一个列表.这个列表包含了输入列表中每一天还有多少天温度升高.如果未来没有升高的情况,则输入 0. # Exam ...
- Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
随机推荐
- js中有哪几种函数?
匿名函数,回调函数,递归函数,构造函数
- loj6277
题解: 树状数组模板提 代码: #include<bits/stdc++.h> using namespace std; ; int num[N],n,a[N],l,r,c,opt; vo ...
- markdown语法模板
(GitHub-Flavored) Markdown Editor Basic useful feature list: Ctrl+S / Cmd+S to save the file Ctrl+Sh ...
- js判断一个值是空的最快方法是不是if(!value){alert("这个变量的值是null");}
!逻辑非 操作符(js)-操作于任何值,(!undefined)(!Null)(!任何对象)(!"")(!"lihuan")(!任何非零数字值) (!0)(!N ...
- 【转】Java中static关键字用法总结
1. 静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法 声明为static的方法有以下几条限制: · 它们仅能调用其他的static 方法. · 它 ...
- asp.net MVC之AuthorizeAttribute浅析
AuthorizeAttribute是asp.net MVC的几大过滤器之一,俗称认证和授权过滤器,也就是判断登录与否,授权与否.当为某一个Controller或Action附加该特性时,没有登录或授 ...
- (C/C++学习笔记) 十九. 模板
十九. 模板 ● 模板的基本概念 模板(template) 函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 语法: template <<模 ...
- CSS特性
css的特性 css具有两大特性:继承性和层叠性 1.继承性 指的是子元素继承父元素的样式,但没有所有的样式都可以继承(那样就太可怕了) 所以具有继承性的属性主要分为三大类 a.文本属性 font-s ...
- 对弈的Python学习笔记
#主要序列类型 str list tuple #列表 list ls=[1,2,3,4]#末尾追加ls.append(5) #添加多个,扩展ls.extend([5,6,7]) #在某个位置插入一个值 ...
- C Runtime Library、C Runtime
C Runtime Library.C Runtime 1)运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些 ...