Golang | Leetcode Golang题解之第225题用队列实现栈

题目:

题解:

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. [力扣题解]225. 队列实现

    2024-07-10 05:34:05       14 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 05:34:05       3 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 05:34:05       4 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 05:34:05       3 阅读
  4. Python语言-面向对象

    2024-07-10 05:34:05       2 阅读

热门阅读

  1. Vue 3与Pinia:下一代状态管理的探索

    2024-07-10 05:34:05       14 阅读
  2. MySQL 中 SQL 查询语句的执行顺序

    2024-07-10 05:34:05       13 阅读
  3. Python之MoviePy视频编辑模块介绍与应用

    2024-07-10 05:34:05       13 阅读
  4. 对Mapper.xml文件进行深入的学习

    2024-07-10 05:34:05       13 阅读
  5. 工作中遇到的问题与解决办法(三)

    2024-07-10 05:34:05       9 阅读
  6. linux工具应用_VERDI

    2024-07-10 05:34:05       11 阅读
  7. 大模型/NLP/算法面试题总结4——bert参数量计算

    2024-07-10 05:34:05       13 阅读
  8. springsecurity(学习自用)

    2024-07-10 05:34:05       10 阅读
  9. 构建响应式CSS导航栏:实现优雅的用户体验

    2024-07-10 05:34:05       10 阅读
  10. debian或Ubuntu中开启ssh允许root远程ssh登录的方法

    2024-07-10 05:34:05       10 阅读