2024/11/26 灵茶题单 单调栈

This commit is contained in:
Cool 2024-11-26 15:34:35 +08:00
parent 5f49f65c77
commit 4b9d1922eb
2 changed files with 31 additions and 2 deletions

View File

@ -2,8 +2,8 @@ package com.cool.hot100.linkedlist;
public class ListNode {
int val;
ListNode next;
public int val;
public ListNode next;
ListNode() {
}

View File

@ -0,0 +1,29 @@
package com.cool.ling_cha_mount.stack;
import com.cool.hot100.linkedlist.ListNode;
import java.util.Deque;
import java.util.LinkedList;
public class Num1019 {
public int[] nextLargerNodes(ListNode head) {
Deque<int[]> stack = new LinkedList<>();
int len = 0;
ListNode node = head;
while (node != null) {
len++;
node = node.next;
}
int[] res = new int[len];
node = head;
int i = 0;
while (node != null) {
while (!stack.isEmpty() && stack.peek()[0] < node.val) {
res[stack.pop()[1]] = node.val;
}
stack.push(new int[]{node.val, i++});
node = node.next;
}
return res;
}
}