Movatterモバイル変換


[0]ホーム

URL:


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

Commit66f2bce

Browse files
authored
Merge pull request6boris#460 from 0xff-dev/382
Add solution and test-cases for problem 382
2 parents0fc153a +477eaed commit66f2bce

File tree

4 files changed

+74
-31
lines changed

4 files changed

+74
-31
lines changed

‎leetcode/301-400/0382.Linked-List-Random-Node/README.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
#[382.Linked List Random Node][title]
22

3-
>[!WARNING|style:flat]
4-
>This question is temporarily unanswered if you have good ideas. Welcome to[Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
##Description
4+
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.
75

8-
**Example 1:**
6+
Implement the`Solution` class:
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
-`Solution(ListNode head)` Initializes the object with the head of the singly-linked list`head`.
9+
-`int getRandom()` Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.
1410

15-
##题意
16-
>...
11+
**Example 1:**
1712

18-
##题解
13+
![example1](./getrand-linked-list.jpg)
1914

20-
###思路1
21-
>...
22-
Linked List Random Node
23-
```go
2415
```
25-
16+
Input
17+
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
18+
[[[1, 2, 3]], [], [], [], [], []]
19+
Output
20+
[null, 1, 3, 2, 2, 3]
21+
22+
Explanation
23+
Solution solution = new Solution([1, 2, 3]);
24+
solution.getRandom(); // return 1
25+
solution.getRandom(); // return 3
26+
solution.getRandom(); // return 2
27+
solution.getRandom(); // return 2
28+
solution.getRandom(); // return 3
29+
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
30+
```
2631

2732
##结语
2833

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
import (
4+
"math/rand"
5+
"sort"
6+
"time"
7+
)
8+
9+
typeListNodestruct {
10+
Valint
11+
Next*ListNode
12+
}
13+
typeSolution382struct {
14+
lengthint
15+
head*ListNode
16+
}
17+
18+
funcConstructor(head*ListNode)Solution382 {
19+
s:=Solution382{length:0,head:head}
20+
forwalker:=head;walker!=nil;walker=walker.Next {
21+
s.length++
22+
}
23+
returns
24+
}
25+
26+
func (this*Solution382)GetRandom()int {
27+
ifthis.head==nil {
28+
return-1
29+
}
30+
seed:=rand.NewSource(time.Now().UnixNano())
31+
rr:=rand.New(seed)
32+
index:=rr.Intn(this.length)
33+
34+
walker:=this.head
35+
for ;walker!=nil&&index>0;walker=walker.Next {
36+
index--
37+
}
38+
returnwalker.Val
39+
}
40+
41+
funcSolution(countint,head*ListNode) []int {
42+
s:=Constructor(head)
43+
ans:=make([]int,count)
44+
fori:=0;i<count;i++ {
45+
ans[i]=s.GetRandom()
46+
}
47+
sort.Ints(ans)
48+
returnans
549
}
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package Solution
22

33
import (
4-
"reflect"
54
"strconv"
65
"testing"
76
)
@@ -10,30 +9,25 @@ func TestSolution(t *testing.T) {
109
//测试用例
1110
cases:= []struct {
1211
namestring
13-
inputsbool
14-
expectbool
12+
countint
13+
head*ListNode
14+
expect []int
1515
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
16+
{"TestCase1",6,&ListNode{Val:1,Next:&ListNode{Val:2,Next:&ListNode{Val:3}}}, []int{1,1,2,2,3,3}},
1917
}
2018

2119
//开始测试
2220
fori,c:=rangecases {
2321
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
25-
if!reflect.DeepEqual(got,c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect,got,c.inputs)
28-
}
22+
Solution(c.count,c.head)
2923
})
3024
}
3125
}
3226

33-
//压力测试
27+
//压力测试
3428
funcBenchmarkSolution(b*testing.B) {
3529
}
3630

37-
//使用案列
31+
//使用案列
3832
funcExampleSolution() {
3933
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp