2024/9/17 LeetCode Hot100 Stack
This commit is contained in:
parent
8999e23702
commit
e3896ccc54
|
@ -0,0 +1,35 @@
|
||||||
|
package com.cool.hot100.stack;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/09/17/17:55
|
||||||
|
* DayNumber 1
|
||||||
|
* Hard 2
|
||||||
|
* Level 6
|
||||||
|
*/
|
||||||
|
public class Num739 {
|
||||||
|
public int[] dailyTemperatures(int[] temperatures) {
|
||||||
|
Deque<Integer> stack = new LinkedList<Integer>();
|
||||||
|
int[] res = new int[temperatures.length];
|
||||||
|
for (int i = 0; i < temperatures.length; i++) {
|
||||||
|
while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) {
|
||||||
|
int temp = stack.pop();
|
||||||
|
res[temp] = i - temp;
|
||||||
|
}
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
dailyTemperatures(new int[]{73,74,75,71,69,72,76,73});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue