Golang | Leetcode Golang 问题解决 #232 使用堆栈实现队列 - 问题解决:
最编程
2024-07-18 17:54:14
...
type MyQueue struct {
inStack, outStack []int
}
func Constructor() MyQueue {
return MyQueue{}
}
func (q *MyQueue) Push(x int) {
q.inStack = append(q.inStack, x)
}
func (q *MyQueue) in2out() {
for len(q.inStack) > 0 {
q.outStack = append(q.outStack, q.inStack[len(q.inStack)-1])
q.inStack = q.inStack[:len(q.inStack)-1]
}
}
func (q *MyQueue) Pop() int {
if len(q.outStack) == 0 {
q.in2out()
}
x := q.outStack[len(q.outStack)-1]
q.outStack = q.outStack[:len(q.outStack)-1]
return x
}
func (q *MyQueue) Peek() int {
if len(q.outStack) == 0 {
q.in2out()
}
return q.outStack[len(q.outStack)-1]
}
func (q *MyQueue) Empty() bool {
return len(q.inStack) == 0 && len(q.outStack) == 0
}
上一篇: 用于图表展示的 Vue+Echarts
推荐阅读
-
OpenCV 高级图形用户界面 (21) 暂停程序执行并等待用户按键进入,使用函数 waitKey - 参数
-
TensorFlow 的基本概念和使用场景
-
桥接模式的解释和代码实现
-
python 机器人编程 - 使用 python API 调用控制 wifi 小车的示例程序
-
Golang 并发编程入门:Goroutine 简介和基本用法
-
使用 cgo 在 Golang 中调用 C 语言库:"runtime/cgo "软件包完整指南 - 摘要
-
使用计时器创建打开屏幕弹出窗口
-
(42) 在 MATLAB 中使用 fftshift 绘制零心功率谱
-
简单 JSON 解析和转储的 C++ 实现
-
宏梦网络编程系列 31--用 RCP 调用 OpenAI 接口实现智能助理