★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-nypljhql-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a blacklist B
containing unique integers from [0, N)
, write a function to return a uniform random integer from [0, N)
which is NOT in B
.git
Optimize it such that it minimizes the call to system’s Math.random()
.github
Note:微信
1 <= N <= 1000000000
0 <= B.length < min(100000, N)
[0, N)
does NOT include N. See interval notation.Example 1:app
Input:
["Solution","pick","pick","pick"]
[[1,[]],[],[],[]] Output: [null,0,0,0]
Example 2:dom
Input:
["Solution","pick","pick","pick"]
[[2,[]],[],[],[]] Output: [null,1,1,1]
Example 3:函數
Input:
["Solution","pick","pick","pick"]
[[3,[1]],[],[],[]] Output: [null,0,0,2]
Example 4:優化
Input:
["Solution","pick","pick","pick"]
[[4,[2]],[],[],[]] Output: [null,1,3,1]
Explanation of Input Syntax:spa
The input is two lists: the subroutines called and their arguments. Solution
's constructor has two arguments, N
and the blacklist B
. pick
has no arguments. Arguments are always wrapped with a list, even if there aren't any.code
給定一個包含 [0,n ) 中獨特的整數的黑名單 B,寫一個函數從 [ 0,n ) 中返回一個不在 B 中的隨機整數。
對它進行優化使其儘可能少調用系統方法 Math.random()
。
提示:
1 <= N <= 1000000000
0 <= B.length < min(100000, N)
[0, N)
不包含 N,詳細參見 interval notation 。示例 1:
輸入: ["Solution","pick","pick","pick"] [[1,[]],[],[],[]] 輸出: [null,0,0,0]
示例 2:
輸入: ["Solution","pick","pick","pick"] [[2,[]],[],[],[]] 輸出: [null,1,1,1]
示例 3:
輸入: ["Solution","pick","pick","pick"] [[3,[1]],[],[],[]] Output: [null,0,0,2]
示例 4:
輸入: ["Solution","pick","pick","pick"] [[4,[2]],[],[],[]] 輸出: [null,1,3,1]
輸入語法說明:
輸入是兩個列表:調用成員函數名和調用的參數。Solution
的構造函數有兩個參數,N
和黑名單 B
。pick
沒有參數,輸入參數是一個列表,即便參數爲空,也會輸入一個 [] 空列表。
1 class Solution { 2 var M:Int = 0 3 var map:[Int:Int] = [Int:Int]() 4 init(_ N: Int, _ blacklist: [Int]) { 5 var N = N 6 for b in blacklist 7 { 8 map[b] = -1 9 } 10 M = N - map.count 11 for b in blacklist 12 { 13 if b < M 14 { 15 while (map[N - 1] != nil) 16 { 17 N -= 1 18 } 19 map[b] = N - 1 20 N -= 1 21 } 22 } 23 } 24 25 func pick() -> Int { 26 var p:Int = Int.random(in:0..<M) 27 return map[p] == nil ? p : map[p]! 28 } 29 } 30 31 /** 32 * Your Solution object will be instantiated and called as such: 33 * let obj = Solution(N, blacklist) 34 * let ret_1: Int = obj.pick() 35 */
1628ms
1 class Solution { 2 3 init(_ N: Int, _ blacklist: [Int]) { 4 M = N - blacklist.count 5 for n in blacklist { 6 mapping[n] = 0 7 } 8 9 var i = M 10 for n in blacklist { 11 if n < M { 12 while mapping[i] != nil { 13 i += 1 14 } 15 mapping[n] = i 16 i += 1 17 } 18 } 19 } 20 21 func pick() -> Int { 22 let r = Int.random(in: 0..<M) 23 if let n = mapping[r] { 24 return n 25 } 26 return r 27 } 28 29 var M : Int 30 var mapping = [Int: Int]() 31 } 32 33 /** 34 * Your Solution object will be instantiated and called as such: 35 * let obj = Solution(N, blacklist) 36 * let ret_1: Int = obj.pick() 37 */