为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode933. 最近的请求次数 | Number of Recent Calls

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

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

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

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

Write a class RecentCounter to count recent requests.

It has only one method: ping(int t), where t represents some time in milliseconds.

Return the number of pings that have been made from 3000 milliseconds ago until now.

Any ping with time in [t - 3000, t] will count, including the current ping.

It is guaranteed that every call to ping uses a strictly larger value of t than before.

Example 1:

Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]

Note:

  1. Each test case will have at most 10000 calls to ping.
  2. Each test case will call ping with strictly increasing values of t.
  3. Each call to ping will have 1 <= t <= 10^9.

写一个 RecentCounter 类来计算最近的请求。

它只有一个方法:ping(int t),其中 t 代表以毫秒为单位的某个时间。

返回从 3000 毫秒前到现在的 ping 数。

任何处于 [t - 3000, t] 时间范围之内的 ping 都将会被计算在内,包括当前(指 t 时刻)的 ping

保证每次对 ping 的调用都使用比之前更大的 t 值。

示例:

输入:inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
输出:[null,1,2,3,3]

提示:

  1. 每个测试用例最多调用 10000 次 ping
  2. 每个测试用例会使用严格递增的 t 值来调用 ping
  3. 每次调用 ping 都有 1 <= t <= 10^9

916ms

 1 class RecentCounter {
 2   var pings: [Int]
 3   var currI: Int
 4   
 5   init() {
 6     pings = []
 7     currI = 0
 8   }
 9   
10   func ping(_ t: Int) -> Int {
11     pings.append(t)
12     
13     while t - pings[currI] > 3000 {
14       currI += 1
15     }
16     
17     return pings.count - currI
18   }
19 }
20 
21 /**
22  * Your RecentCounter object will be instantiated and called as such:
23  * let obj = RecentCounter()
24  * let ret_1: Int = obj.ping(t)
25  */
26  

920ms

 1 class RecentCounter {
 2 
 3     private var callTime: [Int] = []
 4     private var lastFits: Int = 0
 5     private let timeLimite: Int = 3000
 6     
 7     init() {
 8         
 9     }
10 
11     func ping(_ t: Int) -> Int {
12         callTime.append(t)
13         while t - callTime[lastFits] > timeLimite {
14             lastFits += 1
15         }
16         return callTime.count - lastFits
17     }
18 }
19 
20 /**
21  * Your RecentCounter object will be instantiated and called as such:
22  * let obj = RecentCounter()
23  * let ret_1: Int = obj.ping(t)
24  */
25  

944ms

 1 class RecentCounter {
 2 
 3      var nums: [Int]!
 4     
 5     init() {
 6         nums = [Int]()
 7     }
 8     
 9     func ping(_ t: Int) -> Int {
10         nums.append(t)
11         while nums[0] < t - 3000 {
12             nums.remove(at: 0)
13         }
14         return nums.count
15     }
16 }

968ms

 1 class Node {
 2     var val: Int
 3     var next: Node?
 4     init(_ val: Int) {
 5         self.val = val
 6     }
 7 }
 8 
 9 class RecentCounter {
10 
11     var root: Node? 
12     var current: Node?
13     var counter = 0
14     
15     init() {
16         
17     }
18 
19     func ping(_ t: Int) -> Int {
20         var newNode = Node(t)
21         if current == nil {
22             counter += 1
23             root = newNode
24             current = root
25         } else {
26             counter += 1
27             current?.next = newNode
28             current = current?.next
29         }
30         
31         while let val = root?.val, val < t - 3000 {
32             let temp = root?.next 
33             root = temp
34             counter -= 1
35         } 
36         return counter
37     }
38 }
39 
40 /**
41  * Your RecentCounter object will be instantiated and called as such:
42  * let obj = RecentCounter()
43  * let ret_1: Int = obj.ping(t)
44  */
45  

1204ms

 1 class RecentCounter {
 2     var pq:Queue<Int> = Queue<Int>()
 3 
 4     init() {
 5         
 6     }
 7 
 8     func ping(_ t: Int) -> Int {
 9         pq.enQueue(t)
10         while(!pq.isEmpty() && pq.getFirst()! < t-3000)
11         {
12             pq.deQueue()
13         }
14         return pq.count
15     }
16 }
17 
18 public struct Queue<T> {
19     
20     // 泛型数组:用于存储数据元素
21     fileprivate var queue: [T] 
22 
23     // 返回队列中元素的个数
24     public var count: Int {
25         return queue.count
26     }
27     
28     // 构造函数:创建一个空的队列
29     public init() {
30         queue = [T]()
31     }
32     
33     //通过既定数组构造队列
34     init(_ arr:[T]){
35         queue = arr
36     }
37     
38     // 检查队列是否为空
39     // - returns: 如果队列为空,则返回true,否则返回false
40     public func isEmpty() -> Bool {
41         return queue.isEmpty
42     }
43 
44     // 入队列操作:将元素添加到队列的末尾
45     public mutating func enQueue(_ element: T) {
46         queue.append(element)
47     }
48     
49     // 出队列操作:删除并返回队列中的第一个元素
50     public mutating func deQueue() -> T? {
51         return queue.removeFirst()
52     }
53  
54     // 返回队列中的第一个元素(不删除)
55     public func getFirst() -> T? {
56         return queue.first!
57     }
58 }

 

posted @ 2018-11-04 11:28  为敢技术  阅读(511)  评论(0编辑  收藏  举报