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
}
相关推荐
xidianhuihui1 小时前
go install报错: should be v0 or v1, not v2问题解决
开发语言·后端·golang
এ᭄画画的北北8 小时前
力扣-51.N皇后
算法·leetcode
1白天的黑夜18 小时前
前缀和-974.和可被k整除的子数组-力扣(LeetCode)
c++·leetcode·前缀和
8 小时前
LeetCode Hot 100 搜索二维矩阵
算法·leetcode·矩阵
小新学习屋8 小时前
《剑指offer》-算法篇-位运算
python·算法·leetcode·职场和发展·数据结构与算法
鼠鼠一定要拿到心仪的offer9 小时前
Day23-二叉树的层序遍历(广度优先搜素)
数据结构·算法·leetcode
YuTaoShao9 小时前
【LeetCode 热题 100】34. 在排序数组中查找元素的第一个和最后一个位置——二分查找
java·数据结构·算法·leetcode
克里斯蒂亚诺·罗纳尔达10 小时前
vue请求golang后端CORS跨域问题深度踩坑
vue.js·golang
Swift社区10 小时前
从字符串中“薅出”最长子串:LeetCode 340 Swift 解法全解析
算法·leetcode·swift
吃着火锅x唱着歌12 小时前
LeetCode 1616.分割两个字符串得到回文串
算法·leetcode·职场和发展