Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1aa286d

Browse files
committed
feat: add solutions to lc problems: No.0382,0398
* No.0382.Linked List Random Node * No.0398.Random Pick Index
1 parent 1139bb9 commit 1aa286d

File tree

14 files changed

+744
-2
lines changed

14 files changed

+744
-2
lines changed

‎solution/0300-0399/0382.Linked List Random Node/README.md‎

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,168 @@ solution.getRandom();
2828

2929
<!-- 这里可写通用的实现逻辑 -->
3030

31+
蓄水池抽样问题。即从一个包含 n 个对象的列表 S 中随机选取 k 个对象,n 为一个非常大或者不知道的值。通常情况下,n 是一个非常大的值,大到无法一次性把所有列表 S 中的对象都放到内存中。我们这个问题是蓄水池抽样问题的一个特例,即 k=1。
32+
33+
**解法**:我们总是选择第一个对象,以 1/2 的概率选择第二个,以 1/3 的概率选择第三个,以此类推,以 1/m 的概率选择第 m 个对象。当该过程结束时,每一个对象具有相同的选中概率,即 1/n。
34+
35+
**证明**:第 m 个对象最终被选中的概率 P = `选择 m 的概率 ×ばつ 其后面所有对象不被选择的概率`,即:
36+
37+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0382.Linked%20List%20Random%20Node/images/demo.gif"/>
38+
39+
思路同:[398. 随机数索引](/solution/0300-0399/0398.Random%20Pick%20Index/README.md)
40+
3141
<!-- tabs:start -->
3242

3343
### **Python3**
3444

3545
<!-- 这里可写当前语言的特殊实现逻辑 -->
3646

3747
```python
38-
48+
# Definition for singly-linked list.
49+
# class ListNode:
50+
# def __init__(self, val=0, next=None):
51+
# self.val = val
52+
# self.next = next
53+
class Solution:
54+
55+
def __init__(self, head: Optional[ListNode]):
56+
self.head = head
57+
58+
def getRandom(self) -> int:
59+
n = ans = 0
60+
head = self.head
61+
while head:
62+
n += 1
63+
x = random.randint(1, n)
64+
if n == x:
65+
ans = head.val
66+
head = head.next
67+
return ans
68+
69+
70+
# Your Solution object will be instantiated and called as such:
71+
# obj = Solution(head)
72+
# param_1 = obj.getRandom()
3973
```
4074

4175
### **Java**
4276

4377
<!-- 这里可写当前语言的特殊实现逻辑 -->
4478

4579
```java
80+
/**
81+
* Definition for singly-linked list.
82+
* public class ListNode {
83+
* int val;
84+
* ListNode next;
85+
* ListNode() {}
86+
* ListNode(int val) { this.val = val; }
87+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
88+
* }
89+
*/
90+
class Solution {
91+
private ListNode head;
92+
private Random random = new Random();
93+
94+
public Solution(ListNode head) {
95+
this.head = head;
96+
}
97+
98+
public int getRandom() {
99+
int ans = 0, n = 0;
100+
for (ListNode node = head; node != null; node = node.next) {
101+
++n;
102+
int x = 1 + random.nextInt(n);
103+
if (n == x) {
104+
ans = node.val;
105+
}
106+
}
107+
return ans;
108+
}
109+
}
110+
111+
/**
112+
* Your Solution object will be instantiated and called as such:
113+
* Solution obj = new Solution(head);
114+
* int param_1 = obj.getRandom();
115+
*/
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
/**
122+
* Definition for singly-linked list.
123+
* struct ListNode {
124+
* int val;
125+
* ListNode *next;
126+
* ListNode() : val(0), next(nullptr) {}
127+
* ListNode(int x) : val(x), next(nullptr) {}
128+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
129+
* };
130+
*/
131+
class Solution {
132+
public:
133+
ListNode* head;
134+
135+
Solution(ListNode* head) {
136+
this->head = head;
137+
}
138+
139+
int getRandom() {
140+
int n = 0, ans = 0;
141+
for (ListNode* node = head; node != nullptr; node = node->next)
142+
{
143+
n += 1;
144+
int x = 1 + rand() % n;
145+
if (n == x) ans = node->val;
146+
}
147+
return ans;
148+
}
149+
};
150+
151+
/**
152+
* Your Solution object will be instantiated and called as such:
153+
* Solution* obj = new Solution(head);
154+
* int param_1 = obj->getRandom();
155+
*/
156+
```
46157

158+
### **Go**
159+
160+
```go
161+
/**
162+
* Definition for singly-linked list.
163+
* type ListNode struct {
164+
* Val int
165+
* Next *ListNode
166+
* }
167+
*/
168+
type Solution struct {
169+
head *ListNode
170+
}
171+
172+
func Constructor(head *ListNode) Solution {
173+
return Solution{head}
174+
}
175+
176+
func (this *Solution) GetRandom() int {
177+
n, ans := 0, 0
178+
for node := this.head; node != nil; node = node.Next {
179+
n++
180+
x := 1 + rand.Intn(n)
181+
if n == x {
182+
ans = node.Val
183+
}
184+
}
185+
return ans
186+
}
187+
188+
/**
189+
* Your Solution object will be instantiated and called as such:
190+
* obj := Constructor(head);
191+
* param_1 := obj.GetRandom();
192+
*/
47193
```
48194

49195
### **...**

‎solution/0300-0399/0382.Linked List Random Node/README_EN.md‎

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,149 @@ solution.getRandom(); // return 3
5151
### **Python3**
5252

5353
```python
54-
54+
# Definition for singly-linked list.
55+
# class ListNode:
56+
# def __init__(self, val=0, next=None):
57+
# self.val = val
58+
# self.next = next
59+
class Solution:
60+
61+
def __init__(self, head: Optional[ListNode]):
62+
self.head = head
63+
64+
def getRandom(self) -> int:
65+
n = ans = 0
66+
head = self.head
67+
while head:
68+
n += 1
69+
x = random.randint(1, n)
70+
if n == x:
71+
ans = head.val
72+
head = head.next
73+
return ans
74+
75+
76+
# Your Solution object will be instantiated and called as such:
77+
# obj = Solution(head)
78+
# param_1 = obj.getRandom()
5579
```
5680

5781
### **Java**
5882

5983
```java
84+
/**
85+
* Definition for singly-linked list.
86+
* public class ListNode {
87+
* int val;
88+
* ListNode next;
89+
* ListNode() {}
90+
* ListNode(int val) { this.val = val; }
91+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
92+
* }
93+
*/
94+
class Solution {
95+
private ListNode head;
96+
private Random random = new Random();
97+
98+
public Solution(ListNode head) {
99+
this.head = head;
100+
}
101+
102+
public int getRandom() {
103+
int ans = 0, n = 0;
104+
for (ListNode node = head; node != null; node = node.next) {
105+
++n;
106+
int x = 1 + random.nextInt(n);
107+
if (n == x) {
108+
ans = node.val;
109+
}
110+
}
111+
return ans;
112+
}
113+
}
114+
115+
/**
116+
* Your Solution object will be instantiated and called as such:
117+
* Solution obj = new Solution(head);
118+
* int param_1 = obj.getRandom();
119+
*/
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
/**
126+
* Definition for singly-linked list.
127+
* struct ListNode {
128+
* int val;
129+
* ListNode *next;
130+
* ListNode() : val(0), next(nullptr) {}
131+
* ListNode(int x) : val(x), next(nullptr) {}
132+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
133+
* };
134+
*/
135+
class Solution {
136+
public:
137+
ListNode* head;
138+
139+
Solution(ListNode* head) {
140+
this->head = head;
141+
}
142+
143+
int getRandom() {
144+
int n = 0, ans = 0;
145+
for (ListNode* node = head; node != nullptr; node = node->next)
146+
{
147+
n += 1;
148+
int x = 1 + rand() % n;
149+
if (n == x) ans = node->val;
150+
}
151+
return ans;
152+
}
153+
};
154+
155+
/**
156+
* Your Solution object will be instantiated and called as such:
157+
* Solution* obj = new Solution(head);
158+
* int param_1 = obj->getRandom();
159+
*/
160+
```
60161

162+
### **Go**
163+
164+
```go
165+
/**
166+
* Definition for singly-linked list.
167+
* type ListNode struct {
168+
* Val int
169+
* Next *ListNode
170+
* }
171+
*/
172+
type Solution struct {
173+
head *ListNode
174+
}
175+
176+
func Constructor(head *ListNode) Solution {
177+
return Solution{head}
178+
}
179+
180+
func (this *Solution) GetRandom() int {
181+
n, ans := 0, 0
182+
for node := this.head; node != nil; node = node.Next {
183+
n++
184+
x := 1 + rand.Intn(n)
185+
if n == x {
186+
ans = node.Val
187+
}
188+
}
189+
return ans
190+
}
191+
192+
/**
193+
* Your Solution object will be instantiated and called as such:
194+
* obj := Constructor(head);
195+
* param_1 := obj.GetRandom();
196+
*/
61197
```
62198

63199
### **...**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* head;
14+
15+
Solution(ListNode* head) {
16+
this->head = head;
17+
}
18+
19+
int getRandom() {
20+
int n = 0, ans = 0;
21+
for (ListNode* node = head; node != nullptr; node = node->next)
22+
{
23+
n += 1;
24+
int x = 1 + rand() % n;
25+
if (n == x) ans = node->val;
26+
}
27+
return ans;
28+
}
29+
};
30+
31+
/**
32+
* Your Solution object will be instantiated and called as such:
33+
* Solution* obj = new Solution(head);
34+
* int param_1 = obj->getRandom();
35+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
type Solution struct {
9+
head *ListNode
10+
}
11+
12+
func Constructor(head *ListNode) Solution {
13+
return Solution{head}
14+
}
15+
16+
func (this *Solution) GetRandom() int {
17+
n, ans := 0, 0
18+
for node := this.head; node != nil; node = node.Next {
19+
n++
20+
x := 1 + rand.Intn(n)
21+
if n == x {
22+
ans = node.Val
23+
}
24+
}
25+
return ans
26+
}
27+
28+
/**
29+
* Your Solution object will be instantiated and called as such:
30+
* obj := Constructor(head);
31+
* param_1 := obj.GetRandom();
32+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /