我命由我,不由天!


  • 搜索
prometheus docker golang linux kubernetes

leetcode-队列相关题目

发表于 2021-07-10 | 0 | 阅读次数 263

225.用队列实现栈

image.png
代码:

type MyStack struct {
    queueA []int
    queueB []int
}

func Constructor() MyStack {
    return MyStack{}
}

func (this *MyStack) Push(x int)  {
    this.queueB = append(this.queueB,x)
    this.queueB = append(this.queueB,this.queueA...)
    this.queueA =this.queueB
    this.queueB = []int{}
}

func (this *MyStack) Pop() int {
    ret := this.queueA[0]
    this.queueA = this.queueA[1:len(this.queueA)]
    return ret
}

func (this *MyStack) Top() int {
    return this.queueA[0]
}

func (this *MyStack) Empty() bool {
    if len(this.queueA) == 0{
        return true
    } 
    return false
}

思路:

  1. push进来的时候保证一个队列中的顺序是栈的存储顺序
  2. 每次Push一个元素进来时,放到一个新的队列中
  3. 然后将旧的队列元素push到其后面就实现了,队列中的顺序是新->旧的顺序。
  4. 然后放回A队列
  • 本文作者: Dante
  • 本文链接: https://gaodongfei.com/archives/l-e-e-t-c-o-d-e---dui-lie-xiang-guan-ti-mu
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
kubernetes网络
三次握手与四次挥手
  • 文章目录
  • 站点概览
Dante

Dante

119 日志
5 分类
5 标签
RSS
Creative Commons
0%
© 2023 Dante
由 Halo 强力驱动
|
主题 - NexT.Pisces v5.1.4
沪ICP备2020033702号