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

矩阵乘法计算量估算

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

const readline = require("readline")
let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let count = null
let lines = []
let rules = ''
rl.on('line',(line)=>{
    if(!count){
        count = line
    }else{
        if(lines.length<count){
            lines.push(line.split(' ').map(Number))
        }else{
            rules = line
            getTimes(lines,rules)
        }
    }

})

function getTimes(lines,rules){
    let map={}
    let asc = 'A'.charCodeAt()
    lines.forEach((item,index)=>{
        let asc1 = String.fromCharCode(asc+index)
        map[asc1] = item
    })
    let temp =[]
    let times = 0
    for(let i =0;i<rules.length;i++){
        if(rules[i]==='('){
            temp.push(rules[i])
        }
        if(/[A-Z]/.test(rules[i])){
            temp.push(map[rules[i]])
        }
        if(rules[i]===')'){
            let right = temp.pop()
            let left = temp.pop()
            temp.pop()
            let newItem = [left[0],right[1]]
            temp.push(newItem)
            times = times+(left[0]*left[1]*right[1])
        }
    }
    console.log(times)
}

全部评论

相关推荐

2024-11-26 15:41
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
2024-12-29 00:19
快手 Java工程师 26.0k*16.0
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务