思路
js的栈实现是利用Array的push和pop……
function calPoints(ops: string[]): number {
let sta: Array<number> = []
ops.forEach((data: string) => {
switch (data) {
case '+':
sta.push(sta[sta.length - 1] + sta[sta.length - 2])
break
case 'D':
sta.push(sta[sta.length - 1] * 2)
break
case 'C':
sta.pop()
break
default:
sta.push(Number(data))
}
})
return sta.reduce((result: number, index: number) => {
return result + index
})
}
console.log(calPoints(['5', '-2', '4', 'C', 'D', '9', '+', '+']))
评论区