Golang | Leetcode Golang题解之第225题用队列实现栈

题目:

题解:

Go 复制代码
type MyStack struct {
    queue []int
}

/** Initialize your data structure here. */
func Constructor() (s MyStack) {
    return
}

/** Push element x onto stack. */
func (s *MyStack) Push(x int) {
    n := len(s.queue)
    s.queue = append(s.queue, x)
    for ; n > 0; n-- {
        s.queue = append(s.queue, s.queue[0])
        s.queue = s.queue[1:]
    }
}

/** Removes the element on top of the stack and returns that element. */
func (s *MyStack) Pop() int {
    v := s.queue[0]
    s.queue = s.queue[1:]
    return v
}

/** Get the top element. */
func (s *MyStack) Top() int {
    return s.queue[0]
}

/** Returns whether the stack is empty. */
func (s *MyStack) Empty() bool {
    return len(s.queue) == 0
}
相关推荐
_日拱一卒6 分钟前
LeetCode:206反转链表
算法·leetcode·链表
「、皓子~15 分钟前
海狸IM技术升级:从Uniapp到Flutter的跨平台重构之路
flutter·重构·golang·uni-app·im·社交软件
小雅痞25 分钟前
[Java][Leetcode middle] 274. H 指数
java·算法·leetcode
阿里加多9 小时前
第 4 章:Go 线程模型——GMP 深度解析
java·开发语言·后端·golang
GDAL10 小时前
Go Channel 深入全面讲解教程
golang
止语Lab12 小时前
Go GC 十年:一部延迟战争史
golang
阿里加多12 小时前
第 1 章:Go 并发编程概述
java·开发语言·数据库·spring·golang
XiYang-DING15 小时前
【LeetCode】Hash | 136.只出现一次的数字
算法·leetcode·哈希算法
嘻嘻哈哈樱桃17 小时前
俄罗斯套娃信封问题力扣--354
算法·leetcode·职场和发展
田梓燊17 小时前
2026/4/12 leetcode 1320
算法·leetcode·职场和发展