Leetcode 两个队列实现栈 swift
queue1 是最后生成的栈
queue2 是临时队列,把新进来的先放进去,再把queue1里的数据从头到尾读进去,然后互换
class MyStack { var queue1: [Int] = [] var queue2: [Int] = [] init() { } func push(_ x: Int) { queue2.append(x) while !queue1.isEmpty { queue2.append(queue1.removeFirst()) } swap(&queue1, &queue2) } func pop() -> Int { let r: Int = queue1.removeFirst() return r } func top() -> Int { var r: Int = 0 if !queue1.isEmpty { r = queue1.first! } return r } func empty() -> Bool { return queue1.isEmpty } } /** * Your MyStack object will be instantiated and called as such: * let obj = MyStack() * obj.push(x) * let ret_2: Int = obj.pop() * let ret_3: Int = obj.top() * let ret_4: Bool = obj.empty() */
在北京的灯中,有一盏是我家的。这个梦何时可以实现?哪怕微微亮。北京就像魔鬼训练营,有能力的留,没能力的走……
分类:
LeetCode学习
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战