382:链表随机节点

This commit is contained in:
轩辕龙儿 2022-01-16 23:36:16 +08:00
parent e3ade14e2b
commit 4c218cbe88
2 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,104 @@
//给你一个单链表随机选择链表的一个节点并返回相应的节点值每个节点 被选中的概率一样
//
// 实现 Solution
//
//
// Solution(ListNode head) 使用整数数组初始化对象
// int getRandom() 从链表中随机选择一个节点并返回该节点的值链表中所有节点被选中的概率相等
//
//
//
//
// 示例
//
//
//输入
//["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
//[[[1, 2, 3]], [], [], [], [], []]
//输出
//[null, 1, 3, 2, 2, 3]
//
//解释
//Solution solution = new Solution([1, 2, 3]);
//solution.getRandom(); // 返回 1
//solution.getRandom(); // 返回 3
//solution.getRandom(); // 返回 2
//solution.getRandom(); // 返回 2
//solution.getRandom(); // 返回 3
//// getRandom() 方法应随机返回 123中的一个每个元素被返回的概率相等
//
//
//
// 提示
//
//
// 链表中的节点数在范围 [1, 10]
// -10 <= Node.val <= 10
// 至多调用 getRandom 方法 10
//
//
//
//
// 进阶
//
//
// 如果链表非常大且长度未知该怎么处理
// 你能否在不使用额外空间的情况下解决此问题
//
// Related Topics 水塘抽样 链表 数学 随机化 👍 244 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.ListNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//382:链表随机节点
class LinkedListRandomNode {
public static void main(String[] args) {
//测试代码
Solution solution = new LinkedListRandomNode().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
List<Integer> list;
public Solution(ListNode head) {
list = new ArrayList<>();
while (head != null) {
list.add(head.val);
head = head.next;
}
}
public int getRandom() {
Random random = new Random();
int next = random.nextInt() % list.size();
return list.get(next >= 0 ? next : -next);
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,48 @@
<p>给你一个单链表,随机选择链表的一个节点,并返回相应的节点值。每个节点<strong> 被选中的概率一样</strong></p>
<p>实现 <code>Solution</code> 类:</p>
<ul>
<li><code>Solution(ListNode head)</code> 使用整数数组初始化对象。</li>
<li><code>int getRandom()</code> 从链表中随机选择一个节点并返回该节点的值。链表中所有节点被选中的概率相等。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>输入</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>输出</strong>
[null, 1, 3, 2, 2, 3]
<strong>解释</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // 返回 1
solution.getRandom(); // 返回 3
solution.getRandom(); // 返回 2
solution.getRandom(); // 返回 2
solution.getRandom(); // 返回 3
// getRandom() 方法应随机返回 1、2、3中的一个每个元素被返回的概率相等。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>链表中的节点数在范围 <code>[1, 10<sup>4</sup>]</code></li>
<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
<li>至多调用&nbsp;<code>getRandom</code> 方法 <code>10<sup>4</sup></code></li>
</ul>
<p>&nbsp;</p>
<p><strong>进阶:</strong></p>
<ul>
<li>如果链表非常大且长度未知,该怎么处理?</li>
<li>你能否在不使用额外空间的情况下解决此问题?</li>
</ul>
<div><div>Related Topics</div><div><li>水塘抽样</li><li>链表</li><li>数学</li><li>随机化</li></div></div><br><div><li>👍 244</li><li>👎 0</li></div>