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
}
相关推荐
顾琬清14 分钟前
Linux系统Docker部署开源在线协作笔记Trilium Notes与远程访问详细教程
开发语言·后端·golang
rigidwill66614 分钟前
LeetCode hot 100—子集
数据结构·c++·算法·leetcode·职场和发展
阳洞洞20 分钟前
leetcode 322. Coin Change
算法·leetcode·动态规划·完全背包问题
我是唐青枫2 小时前
如何用Go写一个benchmark 解析器及Web UI 数据可视化?
golang
不吃洋葱.3 小时前
力扣448.找到数组中所有消失的元素
数据结构·算法·leetcode
东方窅瞳3 小时前
Bash语言的哈希表
开发语言·后端·golang
编程绿豆侠6 小时前
力扣HOT100之链表:138. 随机链表的复制
算法·leetcode·链表
东方醴歌7 小时前
VMware安装飞牛私有云fnOS并挂载小雅Alist实现异地远程访问
开发语言·后端·golang
LuckyLay8 小时前
LeetCode算法题(Go语言实现)_39
算法·leetcode·golang
Tisfy8 小时前
LeetCode 2843.统计对称整数的数目:字符串数字转换
算法·leetcode·字符串·题解