日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode382. 链表随机节点 | Linked List Random Node

發布時間:2023/12/18 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode382. 链表随机节点 | Linked List Random Node 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:https://www.cnblogs.com/strengthen/p/10282841.html?

Given a singly linked list, return a random node's value from the linked list. Each node must have the?same probability?of being chosen.

Follow up:
What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?

Example:

// Init a singly linked list [1,2,3]. ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); Solution solution = new Solution(head);// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning. solution.getRandom();

給定一個單鏈表,隨機選擇鏈表的一個節點,并返回相應的節點值。保證每個節點被選的概率一樣。

進階:
如果鏈表十分大且長度未知,如何解決這個問題?你能否使用常數級空間復雜度實現?

示例:

// 初始化一個單鏈表 [1,2,3]. ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); Solution solution = new Solution(head);// getRandom()方法應隨機返回1,2,3中的一個,保證每個元素被返回的概率相等。 solution.getRandom();
200ms 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 13 class Solution { 14 var head: ListNode? 15 var current:ListNode? 16 var count = 0 17 18 /** @param head The linked list's head. 19 Note that the head is guaranteed to be not null, so it contains at least one node. */ 20 init(_ head: ListNode?) { 21 self.head = head 22 self.current = head 23 while self.current != nil { 24 count += 1 25 self.current = current?.next 26 } 27 current = head 28 } 29 30 /** Returns a random node's value. */ 31 func getRandom() -> Int { 32 var random = Int.random(in: 0 ..< count) 33 for i in 0..<random { 34 current = current?.next 35 if current == nil { 36 current = head 37 } 38 } 39 return current!.val 40 } 41 } 42 43 /** 44 * Your Solution object will be instantiated and called as such: 45 * let obj = Solution(head) 46 * let ret_1: Int = obj.getRandom() 47 */

356ms

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 13 class Solution { 14 var head: ListNode? 15 16 /** @param head The linked list's head. 17 Note that the head is guaranteed to be not null, so it contains at least one node. */ 18 init(_ head: ListNode?) { 19 self.head = head 20 } 21 22 /** Returns a random node's value. */ 23 func getRandom() -> Int { 24 var arr: [ListNode] = [] 25 var node = head 26 while node != nil { 27 arr.append(node!) 28 node = node?.next 29 } 30 let randomNum = Int.random(in: 0..<arr.count) 31 return arr[randomNum].val 32 } 33 } 34 35 /** 36 * Your Solution object will be instantiated and called as such: 37 * let obj = Solution(head) 38 * let ret_1: Int = obj.getRandom() 39 */

416ms

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 13 class Solution { 14 var head:ListNode? 15 16 /** @param head The linked list's head. 17 Note that the head is guaranteed to be not null, so it contains at least one node. */ 18 init(_ head: ListNode?) { 19 self.head = head 20 } 21 22 /** Returns a random node's value. */ 23 func getRandom() -> Int { 24 var res:Int = head!.val 25 var i:Int = 2 26 var cur:ListNode? = head?.next 27 while(cur != nil) 28 { 29 var j:Int = Int.random(in:0..<i) 30 if j == 0 31 { 32 res = cur!.val 33 } 34 i += 1 35 cur = cur?.next 36 } 37 return res 38 } 39 } 40 41 /** 42 * Your Solution object will be instantiated and called as such: 43 * let obj = Solution(head) 44 * let ret_1: Int = obj.getRandom() 45 */ 46

468ms

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 13 class Solution { 14 15 var head: ListNode? 16 // var vals = [Int]() 17 /** @param head The linked list's head. 18 Note that the head is guaranteed to be not null, so it contains at least one node. */ 19 init(_ head: ListNode?) { 20 self.head = head 21 } 22 23 /** Returns a random node's value. */ 24 func getRandom() -> Int { 25 var curr = head 26 var count = 0 27 var result = 0 28 29 while curr != nil { 30 count += 1 31 if Int.random(in: 0..<count) == 0 { 32 result = curr!.val 33 } 34 curr = curr?.next 35 } 36 37 return result 38 } 39 } 40 41 /** 42 * Your Solution object will be instantiated and called as such: 43 * let obj = Solution(head) 44 * let ret_1: Int = obj.getRandom() 45 */

?

轉載于:https://www.cnblogs.com/strengthen/p/10282841.html

總結

以上是生活随笔為你收集整理的[Swift]LeetCode382. 链表随机节点 | Linked List Random Node的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。