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 ...
随机推荐
- 枚举,Math和Random
1.实用类: 01.枚举 enum Student stu=new Student(); 我们为了保证用户一个健康的输入! 我们使用了封装特性! 用户就不能直接访问我们的属性了!! private c ...
- 阿里云服务器上安装mysql的详细步骤
阿里云安装mysql (1)下载mysql安装包,去mysql官网下载对应的包 mysql数据库官方下载网址: https://downloads.mysql.com/archives/commu ...
- Hibernate多对多映射(双向关联)实例详解——真
一个学生可以选多门课 一门课程有多个学生上 实现步骤: 一.学生 (1)数据库创建学生数据表students,包含id,name字段 设置id字段为主键,类型:bigint,自增 设置name字段,类 ...
- Android : 移植curl-7.61.1 及 openssl-1.1.0i
一.curl-7.61.1 Android平台移植: libcurl是一个免费且易于使用的客户端URL传输库,支持DICT.FILE.FTP.FTPS.Gopher.HTTP.HTTPS.IMAP.I ...
- 进程创建过程详解 CreateProcess
转载请您注明出处:http://www.cnblogs.com/lsh123/p/7405796.html 0x01 CreateProcessW CreateProcess的使用有ANSI版本的Cr ...
- IIC协议解释(转)
IIC协议解释 推荐资源: http://m.elecfans.com/article/574049.html and https://blog.csdn.net/firefl ...
- python学习二三事儿(转,整)
Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...
- countdownlatch 和 CyclicBarrier 和 Semaphore
cdl用的是aqs,共享的是aqs那个volatile的state,阻塞线程列表用的也是aqs的 cb用的是reentrantlock+condition,当然rel用的也是aqs不过不同的是用的是互 ...
- Oracle 一些注意点
number精度问题 Number(p,s): p和s都是可选的. p指精度(precision),即总位数.默认情况下精度为38.精度的取值范围为1~38. s指小数位(scale),小数点右边的位 ...
- nginx——防盗链功能
我们经常会看到在浏览某一图片时会弹出一“403权限禁止”错误,这说明有可能正在浏览的这个网站用到的图片在盗用别的网站图片,而被盗用的网站采用了防盗链技术.那么怎样才能不让自己的网站受害呢? 下面我来介 ...