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
}
相关推荐
赤兔马百花袍3 小时前
php转职golang第一期
golang
chanTwo_003 小时前
go--知识点
开发语言·后端·golang
悟空丶1233 小时前
go基础知识归纳总结
开发语言·后端·golang
阿拉伯的劳伦斯2923 小时前
LeetCode第一题(梦开始的地方)
数据结构·算法·leetcode
ifanatic4 小时前
[Go]-抢购类业务方案
开发语言·后端·golang
桃酥4034 小时前
算法day22|组合总和 (含剪枝)、40.组合总和II、131.分割回文串
数据结构·c++·算法·leetcode·剪枝
山脚ice4 小时前
【Hot100】LeetCode—55. 跳跃游戏
算法·leetcode
桃酥4034 小时前
算法day21|回溯理论基础、77. 组合(剪枝)、216.组合总和III、17.电话号码的字母组合
java·数据结构·c++·算法·leetcode·剪枝
小丁爱养花4 小时前
DFS算法专题(一)——二叉树中的深搜【回溯与剪枝的初步注入】
java·开发语言·算法·leetcode·深度优先·剪枝
伏城之外4 小时前
LeetCode - 15 三数之和
java·javascript·c++·python·leetcode·c