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
}
相关推荐
周杰伦_Jay8 分钟前
【网络编程、架构设计与海量数据处理】网络编程是数据流转的血管,架构设计是系统扩展的骨架,海量数据处理是业务增长的基石。
网络·golang·实时互动·云计算·腾讯云·语音识别
岁忧2 小时前
Go channel 的核心概念、操作语义、设计模式和实践要点
网络·设计模式·golang
夏鹏今天学习了吗3 小时前
【LeetCode热题100(57/100)】括号生成
算法·leetcode·职场和发展
三花聚顶<>3 小时前
310.力扣LeetCode_ 最小高度树_直径法_DFS
算法·leetcode·深度优先
努力学算法的蒟蒻3 小时前
day04(11.2)——leetcode面试经典150
算法·leetcode
Tisfy6 小时前
LeetCode 3217.从链表中移除在数组中存在的节点:哈希表(一次遍历)
leetcode·链表·散列表
Tony Bai6 小时前
从 Python 到 Go:我们失去了什么,又得到了什么?
开发语言·后端·python·golang
小白菜又菜6 小时前
Leetcode 495. Teemo Attacking
算法·leetcode·职场和发展
.柒宇.11 小时前
力扣hot100----15.三数之和(java版)
java·数据结构·算法·leetcode
雪域迷影13 小时前
Go语言中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·后端·http·golang·get