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
}
相关推荐
名誉寒冰1 小时前
【LeetCode】454. 四数相加 II 【分组+哈希表】详解
算法·leetcode·散列表
tao3556672 小时前
【Python刷力扣hot100】49. Group Anagrams
开发语言·python·leetcode
夏鹏今天学习了吗3 小时前
【LeetCode热题100(35/100)】LRU 缓存
算法·leetcode·缓存
Q741_1475 小时前
C++ 位运算 高频面试考点 力扣137. 只出现一次的数字 II 题解 每日一题
c++·算法·leetcode·面试·位运算
墨染点香6 小时前
LeetCode 刷题【103. 二叉树的锯齿形层序遍历、104. 二叉树的最大深度、105. 从前序与中序遍历序列构造二叉树】
算法·leetcode·职场和发展
sitellla7 小时前
Testify Go测试工具包入门教程
git·测试工具·其他·golang
Brookty7 小时前
【算法】二分查找(一)朴素二分
java·学习·算法·leetcode·二分查找
黑色的山岗在沉睡8 小时前
LeetCode 2761. 和等于目标值的质数对
算法·leetcode·职场和发展
T1an-112 小时前
力扣70.爬楼梯
算法·leetcode·职场和发展
T1an-112 小时前
力扣169.多数元素
数据结构·算法·leetcode