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
}
相关推荐
李匠20242 小时前
C++GO语言微服务之图片、短信验证码生成及存储
开发语言·c++·微服务·golang
爱coding的橙子7 小时前
每日算法刷题Day2 5.10:leetcode数组1道题3种解法,用时40min
算法·leetcode
阳洞洞7 小时前
leetcode 18. 四数之和
leetcode·双指针
Kidddddult8 小时前
力扣刷题Day 48:盛最多水的容器(283)
算法·leetcode·力扣
Cxzzzzzzzzzz10 小时前
Kafka Go客户端--Sarama
中间件·golang·kafka·linq
小南家的青蛙11 小时前
LeetCode面试题 01.09 字符串轮转
java·leetcode
元亓亓亓11 小时前
LeetCode热题100--240.搜索二维矩阵--中等
算法·leetcode·矩阵
川川籽12 小时前
hashicorp/raft模块实现的raft集群存在节点跨集群身份冲突问题
golang·go-raft
Asus.Blogs13 小时前
为什么 import _ “github.com/go-sql-driver/mysql“ 要导入但不使用?_ 是什么意思?
sql·golang·github
周Echo周14 小时前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list