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 ...
随机推荐
- 中国队再创佳绩,IOI2018喜获四金
第30届国际信息学奥林匹克竞赛(IOI2018)于9月1日-8日在日本筑波举行,共有来自87个国家(地区)的335名选手参赛. 中国代表队四名选手经过努力拼搏,获得金牌.其中,杨懋龙(湖南长沙市 ...
- python-第一类对象,闭包,迭代器
# def fn(): # print("我叫fn") # fn() # print(fn) # <function fn at 0x0000000001D12E18> ...
- JNA调用DLL(入门):让你一眼就学会
DLL(Dynamic Link Library)文件,是基于C语言的动态链接库文件,就是一些封装好的方法,打成dll格式包,供别人调用 JNA是一种能够使Java语言使调用DLL的一种技术, 首先, ...
- python nltk 安装及配置说明
本教程采用pip安装方式,前期需要在本机安装setuptools 及pip 网上铺天盖地的说了很多关于nltk的说明,特别是后期nltk_data 手动下载操作,多数都不好使,这里整理 用pip安装n ...
- 8.2 C++标准输出流对象
参考:http://www.weixueyuan.net/view/6408.html 总结: iostream头文件,包含了该头文件后,我们就可以直接使用这些对象,包含标准的输出流对象cout.ce ...
- 1.3用socketserver创建服务器
socket服务器代码 # -*- coding: utf-8 -*-import socketserver,time myHost = '' myPort = 50007 def now(): #返 ...
- DevExpress v18.1新版亮点——Reporting篇(二)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Reporting v18.1 的新功能,快来下载试用新版本 ...
- 移动端设置, mobile , 一张图片作为背景 ,平铺 ,自动拉伸 , 图片 铺满视界 ,窗口. background-image , background-size, background-repeat
1. 效果: 浏览器: 手机模拟: 2.代码: <!DOCTYPE html> <html lang="en"> <head> <met ...
- ubantu 安装nginx HTTP反向代理服务器
Nginx发音的“engine x”是一个免费的开源高性能HTTP和反向代理服务器,负责处理互联网上一些最大的网站的负载. 本教程将概述在Ubuntu 18.04机器上安装和管理Nginx的步骤. 安 ...
- Python 时间
import time # 时间戳: 从1970-01-01 00:00:00开始计算. 未来存储的时候用是时间戳 print(time.time()) # 格式化时间 print(time.strf ...