简体中文
Telegram

帖子import java.util.PriorityQueue;

2025年2月9日
D
Deloitte | COGNIZANT | IBM | CAPGEMINI
import java.util.PriorityQueue; import java.util.Scanner; class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } public class Main { public static ListNode mergeKLists(ListNode[] lists) { PriorityQueue<ListNode> minHeap = new PriorityQueue<>((a, b) -> a.val - b.val); for (ListNode list : lists) { if (list != null) { minHeap.add(list); } } ListNode dummy = new ListNode(-1); ListNode current = dummy; while (!minHeap.isEmpty()) { ListNode smallest = minHeap.poll(); current.next = smallest; current = current.next; if (smallest.next != null) { minHeap.add(smallest.next); } } return dummy.next; } public static ListNode arrayToLinkedList(int[] arr) { if (arr.length == 0) return null; ListNode head = new ListNode(arr[0]); ListNode current = head; for (int i = 1; i < arr.length; i++) { current.next = new ListNode(arr[i]); current = current.next; } return head; } public static void printLinkedList(ListNode head) { ListNode current = head; while (current != null) { System.out.print(current.val + " "); current = current.next; } System.out.println(); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int k = scanner.nextInt(); // Number of linked lists ListNode[] lists = new ListNode[k]; scanner.nextLine(); // Move to next line for (int i = 0; i < k; i++) { String[] elements = scanner.nextLine().split(" "); int[] arr = new int[elements.length]; for (int j = 0; j < elements.length; j++) { arr[j] = Integer.parseInt(elements[j]); } lists[i] = arrayToLinkedList(arr); } ListNode mergedHead = mergeKLists(lists); printLinkedList(mergedHead); scanner.close(); } } #MERGE_K_SORTED_ARRAYS #L&T
744 ·

信息流中的邻近内容

DDeloitte | COGNIZANT | IBM | CAPGEMINIWIPRO EXAM ✅✅✅ Contact: @MLCODER2DDeloitte | COGNIZANT | IBM | CAPGEMINIWipro exam ✅✅✅ Contact: @MLCODER2
这条消息
DDeloitte | COGNIZANT | IBM | CAPGEMINIimport java.util.*; class Vehicle { protected String make; protected String model; protected int year; public Vehicle(String make, String model, int year) { thiDDeloitte | COGNIZANT | IBM | CAPGEMINIcase "update car": System.out.print("Enter car model to update: "); String carToUpdate = scanner.nextLine(); if (carInventory.containsKey(ca

来自搜索索引的公开信息流 ChatCrawler ——“公开 Telegram 的 Google”;随着该社群被抓取而更新。 时间为 UTC。

仅限公开内容,使用 Telegram 官方 API。 关于 · 常见问题 · 索引限制 · 移除页面 · 目录 · 搜索 · 计算方法