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
33// #Medium #Top_Interview_Questions #Array #Hash_Table #Math #Design #Randomized
44// #Programming_Skills_II_Day_20 #2022_11_22_Time_1326_ms_(68.23%)_Space_119.7_MB_(83.53%)
55
6- import java.util .Random
6+ import kotlin.random .Random
77
88@Suppress(" kotlin:S2245" )
99class RandomizedSet {
10- private val rand: Random
1110 private val list: MutableList <Int >
1211 private val map: MutableMap <Int , Int >
1312
1413 /* Initialize your data structure here. */
1514 init {
16- rand = Random ()
1715 list = ArrayList ()
1816 map = HashMap ()
1917 }
@@ -45,7 +43,7 @@ class RandomizedSet {
4543
4644 /* Get a random element from the set. */
4745 fun getRandom (): Int {
48- return list[rand .nextInt(list.size)]
46+ return list[Random .nextInt(list.size)]
4947 }
5048}
5149
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ package g0301_0400.s0382_linked_list_random_node
44// #2022_11_24_Time_283_ms_(100.00%)_Space_38.6_MB_(100.00%)
55
66import com_github_leetcode.ListNode
7- import java.util .Random
7+ import kotlin.random .Random
88
99/*
1010 * Example:
@@ -18,12 +18,10 @@ import java.util.Random
1818@Suppress(" NAME_SHADOWING" , " kotlin:S2245" )
1919class Solution (head : ListNode ? ) {
2020 private val al: MutableList <Int >
21- private val rand: Random
2221
2322 init {
2423 var head = head
2524 al = ArrayList ()
26- rand = Random ()
2725 while (head != null ) {
2826 al.add(head.`val `)
2927 head = head.next
@@ -37,7 +35,7 @@ class Solution(head: ListNode?) {
3735 take only the integer part which is a random index.
3836 return the element at that random index.
3937 */
40- val ind = rand .nextInt(al.size)
38+ val ind = Random .nextInt(al.size)
4139 return al[ind]
4240 }
4341}
Original file line number Diff line number Diff line change @@ -3,12 +3,10 @@ package g0301_0400.s0384_shuffle_an_array
33// #Medium #Top_Interview_Questions #Array #Math #Randomized #Algorithm_II_Day_20_Others
44// #2022_11_24_Time_940_ms_(72.09%)_Space_81.5_MB_(51.16%)
55
6- import java.util .Random
6+ import kotlin.random .Random
77
88@Suppress(" kotlin:S2245" )
99class Solution (private val nums : IntArray ) {
10- private val random: Random = Random ()
11-
1210 // Resets the array to its original configuration and return it.
1311 fun reset (): IntArray {
1412 return nums
@@ -18,7 +16,7 @@ class Solution(private val nums: IntArray) {
1816 fun shuffle (): IntArray {
1917 val shuffled = nums.clone()
2018 for (i in nums.size - 1 downTo 1 ) {
21- val j: Int = random .nextInt(i + 1 )
19+ val j: Int = Random .nextInt(i + 1 )
2220 swap(shuffled, i, j)
2321 }
2422 return shuffled
Original file line number Diff line number Diff line change @@ -3,17 +3,15 @@ package g0301_0400.s0398_random_pick_index
33// #Medium #Hash_Table #Math #Randomized #Reservoir_Sampling
44// #2022_11_29_Time_1091_ms_(75.00%)_Space_84.3_MB_(25.00%)
55
6- import java.util .Random
6+ import kotlin.random .Random
77
88@Suppress(" kotlin:S2245" )
99class Solution (nums : IntArray ) {
1010 // O(n) time | O(n) space
1111 private val map: MutableMap <Int , MutableList <Int >>
12- private val rand: Random
1312
1413 init {
1514 map = HashMap ()
16- rand = Random ()
1715 for (i in nums.indices) {
1816 map.computeIfAbsent(
1917 nums[i]
@@ -23,7 +21,7 @@ class Solution(nums: IntArray) {
2321
2422 fun pick (target : Int ): Int {
2523 val list: List <Int > = map[target]!!
26- return list[rand .nextInt(list.size)]
24+ return list[Random .nextInt(list.size)]
2725 }
2826}
2927
You can’t perform that action at this time.
0 commit comments