zoukankan      html  css  js  c++  java
  • leetcode面试题59

    双端队列

    实际上就是一个每次push pop的常规queue和另一个首位是最大值的queue

    type MaxQueue struct {
        Queue     []int
        Max     []int
        Size     int
    }
    
    func Constructor() MaxQueue {
        return MaxQueue{
            Queue: []int{},
            Max:   []int{},
            Size:  0,
        }
    }
    
    
    func (this *MaxQueue) Max_value() int {
        if this.Size == 0 {
            return -1
        }
        return this.Max[0]
    }
    
    
    func (this *MaxQueue) Push_back(value int)  {
        this.Queue = append(this.Queue, value)
        if this.Size == 0 {
            this.Max = append(this.Max, value)
        } else {
            if value > this.Max[0] {
                this.Max = this.Max[0:0]
                this.Max = append(this.Max, value)
                this.Size++
                return
            }
            for i := len(this.Max) - 1; this.Max[i] < value; i-- {
                this.Max = this.Max[:i]
            }
            this.Max = append(this.Max, value)
        }
        this.Size++
    }
    
    
    func (this *MaxQueue) Pop_front() int {
        if this.Size == 0 {
            return -1
        }
        num := this.Queue[0]
        if num == this.Max[0] {
            this.Max = this.Max[1:]
        }
        this.Queue = this.Queue[1:]
        this.Size--
        return num
    }

    end

    一个没有高级趣味的人。 email:hushui502@gmail.com
  • 相关阅读:
    SQLi-Labs
    ASP.NET 简介
    CSS
    Apache 基本配置
    【Windows 基础】 01
    sqli1-4
    SQL注入介绍(本文更新中)
    【DC-1】靶机实战
    常见端口的漏洞总结
    element ui table动态控制某列展示与否,并且该列使用了fixed,导致列表错位问题
  • 原文地址:https://www.cnblogs.com/CherryTab/p/12436403.html
Copyright © 2011-2022 走看看