mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-05 03:36:39 +08:00
【739. Daily Temperatures】【Java】
This commit is contained in:
@ -8,9 +8,11 @@
|
||||
<a href="https://space.bilibili.com/14089380"><img src="https://img.shields.io/badge/B站-@labuladong-000000.svg?style=flat-square&logo=Bilibili"></a>
|
||||
</p>
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
相关推荐:
|
||||
|
||||
* [回溯算法解题套路框架](https://labuladong.gitbook.io/algo)
|
||||
* [动态规划解题套路框架](https://labuladong.gitbook.io/algo)
|
||||
|
||||
@ -50,7 +52,7 @@ vector<int> nextGreaterElement(vector<int>& nums);
|
||||
|
||||
这个问题可以这样抽象思考:把数组的元素想象成并列站立的人,元素大小想象成人的身高。这些人面对你站成一列,如何求元素「2」的 Next Greater Number 呢?很简单,如果能够看到元素「2」,那么他后面可见的第一个人就是「2」的 Next Greater Number,因为比「2」小的元素身高不够,都被「2」挡住了,第一个露出来的就是答案。
|
||||
|
||||

|
||||

|
||||
|
||||
这个情景很好理解吧?带着这个抽象的情景,先来看下代码。
|
||||
|
||||
@ -142,7 +144,7 @@ while (true) {
|
||||
|
||||
**对于这种需求,常用套路就是将数组长度翻倍**:
|
||||
|
||||

|
||||

|
||||
|
||||
这样,元素 3 就可以找到元素 4 作为 Next Greater Number 了,而且其他的元素都可以被正确地计算。
|
||||
|
||||
@ -182,6 +184,10 @@ vector<int> nextGreaterElements(vector<int>& nums) {
|
||||
</p>
|
||||
|
||||
======其他语言代码======
|
||||
|
||||
### java
|
||||
|
||||
```java
|
||||
// 739. Daily Temperatures
|
||||
class Solution {
|
||||
public int[] dailyTemperatures(int[] T) {
|
||||
@ -199,3 +205,5 @@ class Solution {
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user