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

矩阵乘法计算量估算

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

使用栈辅助进行解决,类似于求表达式的解

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] dim = new int[n][2];
        for(int i = 0; i < n; i++){
            dim[i][0] = sc.nextInt();
            dim[i][1] = sc.nextInt();
        }
        
        sc.nextLine();
        String method = sc.nextLine();
        Item[] items = new Item[method.length()];
        int index = 0;
        for(int i = 0; i < method.length(); i++){
            char c = method.charAt(i);
            if(c == '('){
                items[i] = new Item(1);
            }else if(c == ')'){
                items[i] = new Item(2);
            }else{
                items[i] = new Item(3, dim[index][0], dim[index][1]);
                index++;
            }
        }
        int ans = 0;
        Deque<Item> stack = new LinkedList<>();
        for(int i = 0; i < items.length; i++){
            //栈顶    当前
            // (     字符     压栈
            // (     (       压栈
            // 字符   (       压栈
            // 字符   字符    弹栈 计算 在压栈
            // 字符   )       //弹栈两次,将左括号弹出,并且当前元素置为原来栈顶
            // (      )      弹栈
            // )      字符
            //  )     (
            Item item = items[i];
            if(stack.isEmpty()){
                stack.push(item);
            }else if(stack.peek().type==1){
                stack.push(item);
            }else if(item.type == 1 && stack.peek().type == 3){
                stack.push(item);
            }else if(item.type == 3 && stack.peek().type == 3){
                Item A = stack.pop();
                Item B = item;
                Item C = new Item(3, A.m, B.n);
                ans += A.m * B.n * A.n;
                stack.push(C);
            }else if(item.type == 2 && stack.peek().type == 3){
                Item peek = stack.pop();
                stack.pop(); //弹出左括号
                //压入peek
                if(stack.isEmpty() || stack.peek().type == 1){
                    stack.push(peek);
                }else if(stack.peek().type == 3){
                    Item A = stack.pop();
                    Item B = peek;
                    Item C = new Item(3, A.m, B.n);
                    ans += A.m * B.n * A.n;
                    stack.push(C);
                }
            }
        }
        System.out.println(ans);
        
    }
    
    static class Item{
        int type; //0表示( ;1表示) ;3表示矩阵
        int m; //如果是矩阵
        int n; //如果是矩阵
        
        Item(int type){
            this.type = type;
        }
        Item(int type, int m, int n){
            this.type = type;
            this.m = m;
            this.n = n;
        }
    }
    
}
全部评论

相关推荐

02-22 18:38
门头沟学院 Java
程序员牛肉:标准的NPC简历,一个短链接+12306。你可以在牛客上面搜一搜有多少人的简历和你一样。你自己能不能给出你一个理由让面试官在大家简历高度相同的情况下,选择约面你而不是对应的211,985学生? 是因为你即将拥有的那段小厂实习吗?这种小厂实习真的很有含金量吗?因此你可以找实习,但是你如果只能找到小厂实习的话,其实意义不太大。 但你的时间是充足的,相信我:从现在到今年的九月份大三上你就干两个事情:"写博客"+“参加开源之夏”。这两个搞好了不亚于一段大厂实习的含金量。 想要让自己变得更强,首先就是不要把自己当打工人看待,让自己简历上面的活人气息更多一点,不要让自己成为流水线的产物。你不是在出售你的技能,你是在利用你的技能和公司达成一种合作关系。
点赞 评论 收藏
分享
剑桥断刀:找啥工作,牛客找个比如大厂软开或者随便啥的高薪牛马,大把没碰过妹子的技术仔,狠狠拿捏爆金币
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务