• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode710.黑名单中的随机数|RandomPickwithBlacklist

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10506644.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

Optimize it such that it minimizes the call to system’s Math.random().

Note:

  1. 1 <= N <= 1000000000
  2. 0 <= B.length < min(100000, N)
  3. [0, N) does NOT include N. See interval notation.

Example 1:

Input: 
[[1,[]],[],[],[]]
Output: [null,0,0,0]

Example 2:

Input: 
[[2,[]],[],[],[]]
Output: [null,1,1,1]

Example 3:

Input: 
[[3,[1]],[],[],[]]
Output: [null,0,0,2]

Example 4:

Input: 
[[4,[2]],[],[],[]]
Output: [null,1,3,1]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution's constructor has two arguments, N and the blacklist Bpick has no arguments. Arguments are always wrapped with a list, even if there aren't any.


给定一个包含 [0,n ) 中独特的整数的黑名单 B,写一个函数从 [ 0,n ) 中返回一个不在 B 中的随机整数。

对它进行优化使其尽量少调用系统方法 Math.random() 。

提示:

  1. 1 <= N <= 1000000000
  2. 0 <= B.length < min(100000, N)
  3. [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 和黑名单 Bpick 没有参数,输入参数是一个列表,即使参数为空,也会输入一个 [] 空列表。


Runtime: 984 ms
Memory Usage: 23.4 MB
 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  */

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift语言精要-序列化和反序列化发布时间:2022-07-13
下一篇:
[Swift]LeetCode770.基本计算器IV|BasicCalculatorIV发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap