题解 | #矩阵乘法计算量估算#

矩阵乘法计算量估算

https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        LinkedList<MatrixSize> list = new LinkedList<>();
        for (int i = 0; i < num; i++) {
            int rows = scanner.nextInt();
            int line = scanner.nextInt();
            MatrixSize matrixSize = new MatrixSize(rows, line);
            list.add(matrixSize);
        }
        char[] chr = scanner.next().toCharArray();
        Stack<MatrixSize> stack = new Stack<>();
        int result = 0;
        int j = 0;
        for (int i = 0; i < chr.length; i++) {
            if (chr[i] != ')') {
                if (chr[i] != '(') {
                    stack.push(list.get(j));
                    j++;
                }
            } else {
                MatrixSize matrixSizeC = stack.pop();
                MatrixSize matrixSizeD = stack.pop();
                result += matrixSizeC.rows * matrixSizeC.line * matrixSizeD.rows;
                MatrixSize matrixSizeE = new MatrixSize(matrixSizeD.rows, matrixSizeC.line);
                stack.push(matrixSizeE);
            }
        }
        System.out.println(result);
    }
}

class MatrixSize {
    int rows;
    int line;

    public MatrixSize(int rows, int line) {
        this.rows = rows;
        this.line = line;
    }
}

全部评论

相关推荐

头像
11-18 16:08
福州大学 Java
影流之主:干10年不被裁,我就能拿别人一年的钱了,日子有盼头了
点赞 评论 收藏
分享
Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务