1019:链表中的下一个更大节点(代码修改,未完成)

This commit is contained in:
轩辕龙儿 2023-04-10 16:54:51 +08:00
parent 984d29acee
commit a8738e82f3

View File

@ -44,7 +44,9 @@ package leetcode.editor.cn;
import com.code.leet.entiy.ListNode; import com.code.leet.entiy.ListNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue;
//1019:链表中的下一个更大节点 //1019:链表中的下一个更大节点
public class NextGreaterNodeInLinkedList { public class NextGreaterNodeInLinkedList {
@ -64,27 +66,46 @@ public class NextGreaterNodeInLinkedList {
* } * }
*/ */
class Solution { class Solution {
// public int[] nextLargerNodes(ListNode head) {
// List<Integer> list = new ArrayList<>();
// List<Integer> result = new ArrayList<>();
// List<Integer> indexs = new ArrayList<>();
// int max = 0;
// int index = 0;
// while (head != null) {
// indexs.add(index);
// if (head.val > max) {
// int size = indexs.size();
// for (int i = 0; i < size; i++) {
// if (head.val > result.get(indexs.get(i))) {
// result.set(indexs.get(i), head.val);
// indexs.remove(indexs.get(i));
// }
// }
// max = head.val;
// }
// index++;
// result.add(0);
// list.add(head.val);
// head = head.next;
// }
// result.add(0);
// return result.stream().mapToInt(Integer::valueOf).toArray();
// }
public int[] nextLargerNodes(ListNode head) { public int[] nextLargerNodes(ListNode head) {
List<Integer> list = new ArrayList<>();
List<Integer> result = new ArrayList<>(); List<Integer> result = new ArrayList<>();
List<Integer> indexs = new ArrayList<>(); Queue<Integer> queue = new LinkedList<>();
int max = 0; Queue<Integer> indexs = new LinkedList<>();
int index = 0; int index = 0;
while (head != null) { while (head != null) {
indexs.add(index); while (!queue.isEmpty() && head.val > queue.peek()) {
if (head.val > max) { result.set(indexs.poll(), head.val);
int size = indexs.size(); queue.poll();
for (int i = 0; i < size; i++) {
if (head.val > result.get(indexs.get(i))) {
result.set(indexs.get(i), head.val);
indexs.remove(indexs.get(i));
}
}
max = head.val;
} }
index++; queue.add(head.val);
indexs.add(index);
result.add(0); result.add(0);
list.add(head.val); index++;
head = head.next; head = head.next;
} }
result.add(0); result.add(0);