在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, each of the stacks has the same maximum Implement the
Example: Input: ["DinnerPlates","push","push","push","push","push","popAtStack","push","push","popAtStack","popAtStack","pop","pop","pop","pop","pop"] [[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]] Output: [null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1] Explanation: DinnerPlates D = DinnerPlates(2); // Initialize with capacity = 2 D.push(1); D.push(2); D.push(3); D.push(4); D.push(5); // The stacks are now: 2 4 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(0); // Returns 2. The stacks are now: 4 1 3 5 ﹈ ﹈ ﹈ D.push(20); // The stacks are now: 20 4 1 3 5 ﹈ ﹈ ﹈ D.push(21); // The stacks are now: 20 4 21 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(0); // Returns 20. The stacks are now: 4 21 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(2); // Returns 21. The stacks are now: 4 1 3 5 ﹈ ﹈ ﹈ D.pop() // Returns 5. The stacks are now: 4 1 3 ﹈ ﹈ D.pop() // Returns 4. The stacks are now: 1 3 ﹈ ﹈ D.pop() // Returns 3. The stacks are now: 1 ﹈ D.pop() // Returns 1. There are no stacks. D.pop() // Returns -1. There are still no stacks. Constraints:
我们把无限数量 ∞ 的栈排成一行,按从左到右的次序从 0 开始编号。每个栈的的最大容量 实现一个叫「餐盘」的类
示例: 输入: ["DinnerPlates","push","push","push","push","push","popAtStack","push","push","popAtStack","popAtStack","pop","pop","pop","pop","pop"] [[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]] 输出: [null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1] 解释: DinnerPlates D = DinnerPlates(2); // 初始化,栈最大容量 capacity = 2 D.push(1); D.push(2); D.push(3); D.push(4); D.push(5); // 栈的现状为: 2 4 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(0); // 返回 2。栈的现状为: 4 1 3 5 ﹈ ﹈ ﹈ D.push(20); // 栈的现状为: 20 4 1 3 5 ﹈ ﹈ ﹈ D.push(21); // 栈的现状为: 20 4 21 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(0); // 返回 20。栈的现状为: 4 21 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(2); // 返回 21。栈的现状为: 4 1 3 5 ﹈ ﹈ ﹈ D.pop() // 返回 5。栈的现状为: 4 1 3 ﹈ ﹈ D.pop() // 返回 4。栈的现状为: 1 3 ﹈ ﹈ D.pop() // 返回 3。栈的现状为: 1 ﹈ D.pop() // 返回 1。现在没有栈。 D.pop() // 返回 -1。仍然没有栈。 提示:
Runtime: 2024 ms
Memory Usage: 45 MB
1 class DinnerPlates { 2 var map:[Int:[Int]] = [Int:[Int]]() 3 var cap:Int = 0 4 var curr:Int = 0 5 var last:Int = 0 6 var count:Int = 0 7 8 init(_ capacity: Int) { 9 self.cap = capacity 10 map[curr] = [Int]() 11 } 12 13 func push(_ val: Int) { 14 while(map[curr] != nil && map[curr,default:[Int]()].count == cap) 15 { 16 curr += 1 17 } 18 map[curr,default:[Int]()].append(val) 19 last = max(last, curr) 20 count += 1 21 22 } 23 24 func pop() -> Int { 25 if count == 0 {return -1} 26 while(last>=0 && map[last,default:[Int]()].isEmpty) 27 { 28 last -= 1 29 } 30 count -= 1 31 curr = min(curr, last) 32 return map[last,default:[Int]()].removeLast() 33 } 34 35 func popAtStack(_ index: Int) -> Int { 36 if(map[index] == nil || map[index,default:[Int]()].isEmpty) 37 { 38 return -1 39 } 40 count -= 1 41 curr = min(curr, index) 42 return map[index,default:[Int]()].removeLast() 43 } 44 } 45 46 /** 47 * Your DinnerPlates object will be instantiated and called as such: 48 * let obj = DinnerPlates(capacity) 49 * obj.push(val) 50 * let ret_2: Int = obj.pop() 51 * let ret_3: Int = obj.popAtStack(index) 52 */
|
请发表评论