0
点赞
收藏
分享

微信扫一扫

每日算法&面试题,大厂特训二十八天——第十八天(栈|队列)

90哦吼 2022-03-16 阅读 42

目录标题

导读

在这里插入图片描述

肥友们为了更好的去帮助新同学适应算法和面试题,最近我们开始进行专项突击一步一步来。上一期我们完成了动态规划二十一天现在我们进行下一项对各类算法进行二十八天的一个小总结。还在等什么快来一起肥学进行二十八天挑战吧!!

特别介绍

算法特训二十八天

示例 1:

输入:s = "()"
输出:true
示例 2:

输入:s = "()[]{}"
输出:true
示例 3:

输入:s = "(]"
输出:false
示例 4:

输入:s = "([)]"
输出:false
示例 5:

输入:s = "{[]}"
输出:true
class Solution {
    public boolean isValid(String s) {
        int n = s.length();
        if (n % 2 == 1) {
            return false;
        }

        Map<Character, Character> pairs = new HashMap<Character, Character>() {{
            put(')', '(');
            put(']', '[');
            put('}', '{');
        }};
        Deque<Character> stack = new LinkedList<Character>();
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (pairs.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }
}


实现 MyQueue 类:

void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 int peek()
返回队列开头的元素 boolean empty() 如果队列为空,返回 true ;否则,返回 false 说明:

你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is
empty 操作是合法的。 你所使用的语言也许不支持栈。你可以使用 list 或者
deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

示例 1:

输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]

解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

初始部分:
class MyQueue {

    public MyQueue() {

    }
    
    public void push(int x) {

    }
    
    public int pop() {

    }
    
    public int peek() {

    }
    
    public boolean empty() {

    }
}

解题部分:
private int front;

public void push(int x) {
    if (s1.empty())
        front = x;
    while (!s1.isEmpty())
        s2.push(s1.pop());
    s2.push(x);
    while (!s2.isEmpty())
        s1.push(s2.pop());
}


面试题

RabbitMQ 如何保证消息不丢失 ?
生产者:
方案 1:开启 RabbitMQ 事务(同步,性能差)
方案 2:开启 confirm 模式(异步,性能较好)
MQ:(1)exchange 持久化 (2)queue 持久化 (3)消息持久化
消费者:关闭自动 AC

1、TCP 和 UDP 区别?
 TCP 基于连接,UDP 基于无连接。
 TCP 要求系统资源较多,UDP 较少。
 UDP 程序结构较简单。
 TCP 保证数据正确性,UDP 可能丢包。
 TCP 保证数据顺序,UDP 不保证


本文来源于算法面试题特训专栏,这里有大量专业性的算法题比如(动态规划21天,大厂特训28天等等)
欢迎大家一起学习。

链接:传送门
在这里插入图片描述

点击直接资料领取

这里有python,Java学习资料还有有有趣好玩的编程项目,更有难寻的各种资源。反正看看也不亏。

举报

相关推荐

0 条评论