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
}
相关推荐
哎写bug的程序员21 小时前
leetcode复盘(1)
算法·leetcode·职场和发展
雨师@1 天前
ATM 模拟器 Golang 程序--示例
开发语言·后端·golang
qq_534452521 天前
【算法 day02】LeetCode 209.长度最小的子数组 | 59.螺旋矩阵II
java·算法·leetcode·职场和发展
dying_man1 天前
LeetCode--31.下一个排列
算法·leetcode
IC 见路不走1 天前
LeetCode 第75题:颜色分类
数据结构·算法·leetcode
Navigator_Z1 天前
LeetCode //C - 757. Set Intersection Size At Least Two
c语言·算法·leetcode
你怎么知道我是队长1 天前
GO语言---匿名函数
开发语言·后端·golang
不被定义的程序猿1 天前
Golang 在 Linux 平台上的并发控制
开发语言·后端·golang
GalaxyPokemon1 天前
LeetCode - 704. 二分查找
数据结构·算法·leetcode
ifanatic1 天前
[每周一更]-(第147期):使用 Go 语言实现 JSON Web Token (JWT)
前端·golang·json