★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ejageuoh-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Write a class RecentCounter
to count recent requests.git
It has only one method: ping(int t)
, where t represents some time in milliseconds.github
Return the number of ping
s 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.app
Example 1:函數
Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]] Output: [null,1,2,3,3]
Note:測試
10000
calls to ping
.ping
with strictly increasing values of t
.1 <= t <= 10^9
.寫一個 RecentCounter
類來計算最近的請求。spa
它只有一個方法:ping(int t)
,其中 t
表明以毫秒爲單位的某個時間。code
返回從 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]
提示:
10000
次 ping
。t
值來調用 ping
。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 }