2.算法通关面试 --- 堆栈和队列

1.有效的括号
https://leetcode-cn.com/problems/valid-parentheses/

func isValid(s string) bool {
    n := len(s)

    if n % 2 == 1 {
        return false
    }

    pairs := map[byte]byte {
        ')' : '('
        ']' : '['
        '}' : '{'
    }

    stack := []byte {}

    for i := 0; i < n; i++ {
        //说明是右边符号
        if pairs[s[i]] > 0 {
            //如果栈是空的,说明是错误的 || 非空,看栈顶元素是否等于相对应的左边符号,不等于是错的返回。
            if len(stack) == 0 || stack[len(stack) - 1] != pairs[s[i]] {
                return false
            }

            //等于的话,出栈
            stack = stack[:len(stack) - 1]
        } else {
            //左边符号,直接入栈
            stack = append(stack, s[i])
        }
    }

    //栈长度等于0说明是对的。
    return len(stack) == 0
}

2.用栈实现队列
https://leetcode-cn.com/problems/implement-queue-using-stacks/

type MyQueue struct {
    inStack, outStack []int
}

func Constructor() MyQueue {
    return MyQueue{}
}

func (q *MyQueue) Push(x int) {
    q.inStack = append(q.inStack, x)
}

func (q *MyQueue) in2out() {
    for len(q.inStack) > 0 {
        q.outStack = append(q.outStack, q.inStack[len(q.inStack) - 1])
        q.inStack = q.inStack[:len(q.inStack) - 1]
    }
}

func (q *MyQueue) Pop() int {
    if len(q.outStack) == 0 {
        q.in2out()
    }

    x := q.outStack[len(q.outStack) - 1]
    q.outStack = q.outStack[:len(q.outStack) - 1]

    return x
}

func (q *MyQueue) Peek() int {
    if len(q.outStakc) == 0{
        q.in2out()
    }

    return q.outStakc[len(q.outStack) - 1]
}

func (q *MyQueue) Empty() bool {
    return len(q.inStack) == 0 && len(q.outStack) == 0
}

3.用队列实现栈
https://leetcode-cn.com/problems/implement-stack-using-queues/

type MyStack struct {
    queue1, queue2 []int
}

func Constructor() (s MyStack) {
    return 
} 

func (s *MyStack) Push(x int) {
    s.queue2 = append(s.queue2, x)

    for len(s.queue1) > 0 {
        s.queue2 = append(s.queue2, s.queue1[0])
        s.queue1 = s.queue1[1:]
    }

    s.queue1, s.queue2 = s.queue2, s.queue1
}

func (s *MyStack) Pop() int {
    v := s.queue1[0]
    s.queue1 = s.queue1[1:]

    return v
}

func (s *MyStack) Top int {
    return s.queue1[0]
}

func (s *MyStack) Empty() bool {
    return len(s.queue1) == 0
}


优先队列

1.数据流中的第K大元素
https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/

type KthLargest struct {
    sort.IntSlice
    k int
}

func Constructor(k int, nums []int) KthLargest {
    kl := KthLargest{k: k}

    for _, val := range nums {
        kl.Add(val)
    }

    return kl
}

func (kl *KthLargest) Push(v interfact{}) {
    kl.IntSlice = append(kl.IntSlice, v.(int))
} 

func (kl *KthLargest) Pop() {
    a := kl.IntSlice
    v := a[len(a) - 1]
    kl.IntSlice = a[:len(a) - 1]

    return v
} 

func (kl *KthLargest) Add(val int) {
    heap.Push(kl, val)

    if kl.Len() > kl.k {
        heap.Pop(kl)
    }

    return kl.IntSlice[0]
} 

2.滑动窗口最大值
https://leetcode-cn.com/problems/sliding-window-maximum/

https://www.bigocheatsheet.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值