只是想知道是否有人能夠幫助我解決 Hackerrank 問題https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list/problem
我找到了以下解決方案,該解決方案僅通過了 8 個測驗中的 3 個。
我對自己做錯了什么感到困惑,因為當我搜索互聯網時,我發現了一個非常相似的 Java 解決方案 - https://www.geeksforgeeks.org/insert-value-sorted-way-sorted-doubly-鏈表/
任何幫助將不勝感激,我不知所措。
fun sortedInsert(llist: DoublyLinkedListNode?, data: Int): DoublyLinkedListNode? {
val node = DoublyLinkedListNode(data)
if (llist == null) return node
if (llist.data >= data) {
node.next = llist
node.next!!.prev = node
return node
}
var current = llist
while (current?.next != null && current.next!!.data < data) {
current = current.next
}
node.next = current?.next
if (current?.next != null) current.next!!.prev = node
node.prev = current
current?.next = node
return llist
}
uj5u.com熱心網友回復:
HackerRank 的測驗工具對于 Kotlin 來說似乎被破壞了,缺少一個 println 來為每個測驗用例分離輸出。一些通過(包括示例測驗)的原因是t=1這些,所以不會觸發錯誤。
有關該問題的更多投訴,請參閱問題的討論主題。一些投訴可以追溯到 2021 年 12 月的 3 年前,這表明這不是 HR 需要解決的優先事項,如果他們甚至意識到這個問題的話。此外,該問題似乎會影響 Kotlin 中其他鏈表問題中的樣板檔案,例如Reverse a Doubly Linked List。
這是您翻譯成 Java 15 的代碼,它通過了 HackerRank 的判斷:
public static DoublyLinkedListNode sortedInsert(
DoublyLinkedListNode llist, int data
) {
var node = new DoublyLinkedListNode(data);
if (llist == null) return node;
if (llist.data >= data) {
node.next = llist;
node.next.prev = node;
return node;
}
var current = llist;
while (current.next != null && current.next.data < data) {
current = current.next;
}
node.next = current.next;
if (current.next != null) current.next.prev = node;
node.prev = current;
current.next = node;
return llist;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/377843.html
上一篇:獲取字串中插入字符的所有組合
