File tree Expand file tree Collapse file tree 4 files changed +8
-16
lines changed
src/main/kotlin/g0301_0400
s0380_insert_delete_getrandom_o1
s0382_linked_list_random_node Expand file tree Collapse file tree 4 files changed +8
-16
lines changed Original file line number Diff line number Diff line change @@ -3,17 +3,15 @@ package g0301_0400.s0380_insert_delete_getrandom_o1
3
3
// #Medium #Top_Interview_Questions #Array #Hash_Table #Math #Design #Randomized
4
4
// #Programming_Skills_II_Day_20 #2022_11_22_Time_1326_ms_(68.23%)_Space_119.7_MB_(83.53%)
5
5
6
- import java.util .Random
6
+ import kotlin.random .Random
7
7
8
8
@Suppress(" kotlin:S2245" )
9
9
class RandomizedSet {
10
- private val rand: Random
11
10
private val list: MutableList <Int >
12
11
private val map: MutableMap <Int , Int >
13
12
14
13
/* Initialize your data structure here. */
15
14
init {
16
- rand = Random ()
17
15
list = ArrayList ()
18
16
map = HashMap ()
19
17
}
@@ -45,7 +43,7 @@ class RandomizedSet {
45
43
46
44
/* Get a random element from the set. */
47
45
fun getRandom (): Int {
48
- return list[rand .nextInt(list.size)]
46
+ return list[Random .nextInt(list.size)]
49
47
}
50
48
}
51
49
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ package g0301_0400.s0382_linked_list_random_node
4
4
// #2022_11_24_Time_283_ms_(100.00%)_Space_38.6_MB_(100.00%)
5
5
6
6
import com_github_leetcode.ListNode
7
- import java.util .Random
7
+ import kotlin.random .Random
8
8
9
9
/*
10
10
* Example:
@@ -18,12 +18,10 @@ import java.util.Random
18
18
@Suppress(" NAME_SHADOWING" , " kotlin:S2245" )
19
19
class Solution (head : ListNode ? ) {
20
20
private val al: MutableList <Int >
21
- private val rand: Random
22
21
23
22
init {
24
23
var head = head
25
24
al = ArrayList ()
26
- rand = Random ()
27
25
while (head != null ) {
28
26
al.add(head.`val `)
29
27
head = head.next
@@ -37,7 +35,7 @@ class Solution(head: ListNode?) {
37
35
take only the integer part which is a random index.
38
36
return the element at that random index.
39
37
*/
40
- val ind = rand .nextInt(al.size)
38
+ val ind = Random .nextInt(al.size)
41
39
return al[ind]
42
40
}
43
41
}
Original file line number Diff line number Diff line change @@ -3,12 +3,10 @@ package g0301_0400.s0384_shuffle_an_array
3
3
// #Medium #Top_Interview_Questions #Array #Math #Randomized #Algorithm_II_Day_20_Others
4
4
// #2022_11_24_Time_940_ms_(72.09%)_Space_81.5_MB_(51.16%)
5
5
6
- import java.util .Random
6
+ import kotlin.random .Random
7
7
8
8
@Suppress(" kotlin:S2245" )
9
9
class Solution (private val nums : IntArray ) {
10
- private val random: Random = Random ()
11
-
12
10
// Resets the array to its original configuration and return it.
13
11
fun reset (): IntArray {
14
12
return nums
@@ -18,7 +16,7 @@ class Solution(private val nums: IntArray) {
18
16
fun shuffle (): IntArray {
19
17
val shuffled = nums.clone()
20
18
for (i in nums.size - 1 downTo 1 ) {
21
- val j: Int = random .nextInt(i + 1 )
19
+ val j: Int = Random .nextInt(i + 1 )
22
20
swap(shuffled, i, j)
23
21
}
24
22
return shuffled
Original file line number Diff line number Diff line change @@ -3,17 +3,15 @@ package g0301_0400.s0398_random_pick_index
3
3
// #Medium #Hash_Table #Math #Randomized #Reservoir_Sampling
4
4
// #2022_11_29_Time_1091_ms_(75.00%)_Space_84.3_MB_(25.00%)
5
5
6
- import java.util .Random
6
+ import kotlin.random .Random
7
7
8
8
@Suppress(" kotlin:S2245" )
9
9
class Solution (nums : IntArray ) {
10
10
// O(n) time | O(n) space
11
11
private val map: MutableMap <Int , MutableList <Int >>
12
- private val rand: Random
13
12
14
13
init {
15
14
map = HashMap ()
16
- rand = Random ()
17
15
for (i in nums.indices) {
18
16
map.computeIfAbsent(
19
17
nums[i]
@@ -23,7 +21,7 @@ class Solution(nums: IntArray) {
23
21
24
22
fun pick (target : Int ): Int {
25
23
val list: List <Int > = map[target]!!
26
- return list[rand .nextInt(list.size)]
24
+ return list[Random .nextInt(list.size)]
27
25
}
28
26
}
29
27
You can’t perform that action at this time.
0 commit comments