博客记录-day171-力扣+Netty+微服务

0 阅读4分钟

一、力扣

1、有效的括号字符串

678. 有效的括号字符串

image.png

image.png

class Solution {
    public boolean checkValidString(String s) {
        // 初始化最小和最大未匹配左括号数
        int minCount = 0; // 表示当前可能的最少剩余左括号数(考虑*作为右括号的情况)
        int maxCount = 0; // 表示当前可能的最多剩余左括号数(考虑*作为左括号的情况)
        
        for (int i = 0; i < s.length(); i++) {
            char temp = s.charAt(i);
            
            if (temp == '(') {
                // 遇到左括号,两种计数都增加
                minCount++;
                maxCount++;
            } else if (temp == ')') {
                // 遇到右括号,两种计数都减少
                minCount--;
                maxCount--;
            } else {
                // 遇到星号,可视为左/右括号或空,因此:
                // - 最多左括号数+1(当视为左括号)
                // - 最少左括号数-1(当视为右括号)
                minCount--;
                maxCount++;
            }
            
            // 确保最小未匹配数不小于0(不能有负数的未匹配左括号)
            minCount = Math.max(minCount, 0);
            
            // 如果最大未匹配数小于0,说明无论如何都无法平衡,提前返回false
            if (maxCount < 0) {
                return false;
            }
        }
        
        // 最终所有括号必须能够完全匹配(最小未匹配数为0)
        return minCount == 0;
    }
}

2、柱状图中最大的矩形

84. 柱状图中最大的矩形

image.png

class Solution {
    public int largestRectangleArea(int[] heights) {
        int n = heights.length;
        int[] left = new int[n];
        int[] right = new int[n];
        ArrayDeque<Integer> queue = new ArrayDeque<>();
        for (int i = 0; i < n; i++) {
            while (!queue.isEmpty() && heights[queue.peek()] >= heights[i]) {
                queue.pop();
            }
            left[i] = -1;
            if (!queue.isEmpty()) {
                left[i] = queue.peek();
            }
            queue.push(i);
        }
        queue = new ArrayDeque<>();
        for (int i = n - 1; i >= 0; i--) {
            while (!queue.isEmpty() && heights[queue.peek()] >= heights[i]) {
                queue.pop();
            }
            right[i] = n;
            if (!queue.isEmpty()) {
                right[i] = queue.peek();
            }
            queue.push(i);
        }
        int res = 0;
        for (int i = 0; i < n; i++) {
            res = Math.max(res, heights[i] * (right[i] - left[i] - 1));
        }
        return res;
    }
}

3、课程表

207. 课程表

image.png

class Solution {
    List<Integer>[] path;
    int[] color;

    public boolean canFinish(int numCourses, int[][] prerequisites) {
        path = new List[numCourses];
        color = new int[numCourses];
        Arrays.setAll(path, e -> new ArrayList<>());
        for (var e : prerequisites) {
            path[e[1]].add(e[0]);
        }
        for (int i = 0; i < numCourses; i++) {
            if (color[i] == 0 && !dfs(i)) {
                return false;
            }
        }
        return true;
    }

    public boolean dfs(int x) {//true为没有环
        color[x] = 1;
        for (int nex : path[x]) {
            if (color[nex] == 1 || (color[nex] == 0 && !dfs(nex))) {
                return false;
            }
        }
        color[x] = 2;
        return true;
    }
}

4、24 点游戏

679. 24 点游戏

image.png

image.png

class Solution {
    // 定义目标数值24和浮点数精度误差范围
    private static final double TARGET = 24;
    private static final double EPISLON = 1e-6;
    
    public boolean judgePoint24(int[] cards) {
        // 将输入的整型数组转换为双精度浮点数组,并启动递归判断
        return helper(new double[]{ cards[0], cards[1], cards[2], cards[3] });
    }
    
    private boolean helper(double[] nums) {
        // 递归终止条件:只剩单个数字时,检查是否接近目标值(考虑浮点精度)
        if (nums.length == 1) return Math.abs(nums[0] - TARGET) < EPISLON;
        
        // 遍历所有可能的两数组合(i和j不同)
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                // 创建新数组保存:剩余未使用的数字 + 当前两数的运算结果
                double[] next = new double[nums.length - 1];
                
                // 将未参与运算的数字复制到新数组
                for (int k = 0, pos = 0; k < nums.length; k++) {
                    if (k != i && k != j) {
                        next[pos++] = nums[k];
                    }
                }
                // 对两数进行所有可能的运算,并递归验证后续可能性
                for (double num : calculate(nums[i], nums[j])) {
                    next[next.length - 1] = num;  // 将运算结果填入新数组末尾
                    if (helper(next)) return true; // 若任意分支成功则立即返回
                }
            }
        }
        return false; // 所有组合尝试失败
    }
    
    private List<Double> calculate(double a, double b) {
        List<Double> list = new ArrayList<>();
        list.add(a + b);      // 加法
        list.add(a - b);      // 减法(a - b)
        list.add(b - a);      // 减法(b - a)
        list.add(a * b);      // 乘法
        
        // 除法需处理除数为零的情况(使用精度阈值判断)
        if (!(Math.abs(b) < EPISLON)) {  
            list.add(a / b);  // 除法(a / b)
        }
        if (!(Math.abs(a) < EPISLON)) {  
            list.add(b / a);  // 除法(b / a)
        }
        return list;
    }
}

5、通配符匹配

44. 通配符匹配

image.png

class Solution {
    public boolean isMatch(String s, String p) {
        int m = s.length(), n = p.length();
        boolean[][] dp = new boolean[m + 1][n + 1];
        dp[0][0] = true;
        for (int i = 1; i <= n; i++) {
            if (p.charAt(i - 1) == '*') {
                dp[0][i] = true;
            } else {
                break;
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (p.charAt(j) == '?') {
                    dp[i + 1][j + 1] = dp[i][j];
                } else if (p.charAt(j) == '*') {
                    dp[i + 1][j + 1] = dp[i + 1][j] || dp[i][j + 1];
                } else {
                    if (p.charAt(j) == s.charAt(i)) {
                        dp[i + 1][j + 1] = dp[i][j];
                    }
                }
            }
        }
        return dp[m][n];
    }
}

二、语雀-Netty

1、为什么Netty适合做网络编程?

✅为什么Netty适合做网络编程?

image.png

2、Netty性能好的原因是什么?

Netty性能好的原因是什么?

image.png

3、什么是零拷贝?

✅什么是零拷贝?

image.png

1. DMA

image.png

2. mmap

image.png

image.png

4、Netty的零拷贝是怎么实现的?

✅Netty的零拷贝是怎么实现的?

image.png

image.png

5、能不能说一说Netty的无锁化设计?

✅能不能说一说Netty的无锁化设计?

image.png

6、Netty的线程模型是怎么样的?

✅Netty的线程模型是怎么样的?

image.png

1. 单Reactor单线程模型

image.png

2. 单Reactor多线程模型

image.png

3. 主从Reactor模型

image.png

7、Netty如何解决TCP粘包、拆包的问题的?

✅Netty如何解决TCP粘包、拆包的问题的?

image.png

8、Netty的Buffer为什么好用

✅Netty的Buffer为什么好用

image.png

image.png

9、说说 Netty 的对象池技术?

✅说说 Netty 的对象池技术?

image.png

image.png

10、Netty有哪些序列化协议?

✅Netty有哪些序列化协议?

image.png

三、语雀-微服务

1、分布式和微服务的区别是什么?

✅分布式和微服务的区别是什么?

image.png

2、什么是微服务架构?优势?特点?

✅什么是微服务架构?优势?特点?

image.png

3、限流、降级、熔断有什么区别?

✅限流、降级、熔断有什么区别?

1. 限流

image.png

2. 降级

image.png

3. 熔断

image.png