739. Daily Temperatures
为什么80%的码农都做不了架构师?>>>
Description
Tag:Stack, Hash Table
Difficulty: Medium
Given a list of daily temperatures T, return a list such 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 of temperatures T = [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].
Solution
从后向前,将大值压入栈中,在压栈过程中,将小值pop出去,然后依次查找即可。
type ele struct {index inttemperature int
}
var stack []ele
var top = -1
func dailyTemperatures(T []int) []int {size := len(T)stack = make([]ele, size)result := make([]int, size)for i:= size -1; i>=0;i-- {result[i] = find(T[i], i)push(T[i], i)}return result
}func find(t int, index int) int {for i:=top; i>=0; i-- {if stack[i].temperature > t {return stack[i].index - index}}return 0
}func push(t int, i int) {for top >=0 && stack[top].temperature <= t {top--}top++element := ele{i, t}stack[top] = element
}
转载于:https://my.oschina.net/liufq/blog/2961411
739. Daily Temperatures相关推荐
- 739. Daily Temperatures - LeetCode
Question 739. Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: p ...
- LeetCode #739 - Daily Temperatures
题目描述: Given a list of daily temperatures T, return a list such that, for each day in the input, tell ...
- 69.Daily Temperatures(日常气温)
Level: Medium 题目描述: Given a list of daily temperatures T, return a list such that, for each day in ...
- LeetCode739. Daily Temperatures
题目: Given a list of daily temperatures, produce a list that, for each day in the input, tells you ho ...
- 单调队列 Monotonic Queue / 单调栈 Monotonic Stack
2018-11-16 22:45:48 一.单调队列 Monotone Queue 239. Sliding Window Maximum 问题描述: 问题求解: 本题是一个经典的可以使用双端队列或者 ...
- leetcode刷题规划
LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...
- LeetCode刷题之python解法(持续更新)
1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...
- Leetcode算法题-解法转载
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...
- Leetcode题解(超赞!!!)
我是技术搬运工,好东西当然要和大家分享啦.原文地址 算法思想 二分查找 二分查找思想简单,但是在实现时有一些需要注意的细节: 在计算 mid 时不能使用 mid = (l + h) / 2 这种方式, ...
最新文章
- 028_jdbc-mysql大文本
- pytorch保存模型时报错***object has no attribute 'state_dict'
- html 正则表达式验证金额,js金额校验,js正则表达式,包含正负,小数点后两位...
- 2020年日历电子版(打印版)_灵感 | 快来康康——2020年的日历设计的这么有创意?...
- 【SQL】SELECT 语句
- python小说爬虫练习
- 【ZigBee】JN5169 ZigBee3.0设备入网过程
- [ERR] 1273 - Unknown collation: ‘utf8mb4_0900_ai_ci‘
- 她力量系列四丨读博6年两次换导师,靠一点点“倔”,俞舟成为social chatbot的开拓者之一
- 供应链金融你了解多少?
- #vue#element-ui文件上传(格式校验)
- the mid-autumn festival
- 打印机在不同操作系统的打印区别
- 微营销培训,微营销师职业培训
- 你有Gmail邮箱吗?
- T46:字符串转换成整数(Java)
- ZooKeeper——Curator客户端基本使用(笔记)
- VC 资源文件 Resource.h *.rc *.rc2
- java怎么跑游戏_RunGame java赛跑小游戏源代码和素材,适合新手入门练手。 Develop 272万源代码下载- www.pudn.com...
- 【JavaScript 逆向】521 加速乐多层响应 Cookie 逆向