[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相关推荐
- leetcode 382. Linked List Random Node | 382. 链表随机节点(Java)
题目 https://leetcode.com/problems/linked-list-random-node/ 题解 先存起来,再随机返回即可.不知道在考察什么? /*** Definition ...
- LeetCode 382. 链表随机节点(概率)
1. 题目 给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样. 进阶: 如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现? 来源:力扣( ...
- Leetcode 382. 链表随机节点 解题思路及C++实现
解题思路: 因为题目中要求需要常数级的空间复杂度,所以就需要计算链表长度了. /*** Definition for singly-linked list.* struct ListNode {* i ...
- LeetCode Linked List Random Node(蓄水池采样算法)
问题:给出一个单链表,随机选择链表中的一个节点,返回相应的值.保证每个节点被选的概率一样 思路:每次只保留一个数,当遇到第 i 个数时,以 1/i的概率保留它,(i-1)/i的概率保留原来的数. 具体 ...
- 每天一道LeetCode-----复制一个带有随机节点的链表
Copy List with Random Pointer 原题链接Copy List with Random Pointer 深拷贝一个链表,链表节点的成员包括 指向下一个节点的指针 指向一个随机节 ...
- 自回避随机行走问题 c语言,醉汉随机行走/随机漫步问题(Random Walk Randomized Algorithm Python)...
世界上有些问题看似是随机的(stochastic),没有规律可循,但很可能是人类还未发现和掌握这类事件的规律,所以说它们是随机发生的. 随机漫步(Random Walk)是一种解决随机问题的方法,它 ...
- 单链表(Singly LInked LIst)
单链表(Singly Linked List) 单链表是比较常见的链表类型.其中每个节点都有一个指向序列下一个节点的指针.这意味着,对链表执行遍历时,只能在一个方向上从头到尾的遍历:为了能够访问链表中 ...
- Leetcode-链表随机节点
原题:链表随即节点 分析: 本题使用的是水塘算法进行随机选择.如何理解? 举个例子: 池子里如果有一个数字,则这个数字被取到的概率是100% 池子里如果有两个数字,则这个数字被取到的概率是50% 如果 ...
- C#LeetCode刷题之#707-设计链表(Design Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4118 访问. 设计链表的实现.您可以选择使用单链表或双链表.单链 ...
最新文章
- 电气6机30节点数据介绍(常适用于优化调度)
- 搜索引擎索引之索引基础
- Java Optional学习笔记
- java vo转map_JAVA Map转换为Bean或VO
- 深圳市收运体系运营管理_华为公司质量管理体系构建和运营实践
- nodejs mysql模块_NodeJs使用Mysql模块实现事务处理
- 最好用的虚拟机软件----VMware详细图文教程
- 最全eclipse自由轻松来回切换项目的jdk版本的方法
- Linux 超级漂亮的 Shell
- AAtitit.随时间变色特效 ---包厢管理系统的规划titit.随
- 多缝夫琅禾费衍射matlab,用matlab实现夫琅禾费多缝衍射代码
- Flink的State与Rescale
- onblur 与onclick 冲突;onblur导致onclick事件丢失
- 微信小程序 —— 自定义picker选择器弹窗内容
- 有限新息率FRI信号模型
- DIAL算法计算单位流率-python实现
- 计算机键盘的功能键在哪,台式电脑home键在哪里(有关电脑按键作用介绍)
- 2020新区势峰会精彩回顾,预见“金融科技+区块链”的下一站
- CANDENCE :如何新建PCB文件、设置PCB画布尺寸、绘制PCB板框
- 准时下班系列_Access合集之第2集—三种方案批量上传图片到数据库,你会几种?
热门文章
- SAP CRM 中间件里的发布-订阅者模式
- AC自动机build时的一点小优化
- php trace 图形,trace工具定位php性能问题
- 个人博客管理系统_教程 | 一文搭建你的第一个免费专属博客
- uap连接oracle,UAP环境初始化操作手册Oralce版.pdf
- mysql 递归查找父节点_MYSQL递归查询,根据子类ID查询所有父类(最全)
- android壁纸选择器,Android 图片选择器
- android studio socket编程实例
- 查看linux文件系统结构,linux的文件系统架构
- php mysqli不识别,不识别数据库PHP MYSQLi中的密码