You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 构造一个简单的栈,包含 push, pop, top 操作functionStack(){this.v=[]}Stack.prototype.top=function(){returnthis.v[this.v.length-1]}Stack.prototype.push=function(e){this.v.push(e)}Stack.prototype.pop=function(){returnthis.v.pop()}varcalPoints=function(ops){constpoints=newStack()lettotal=0constswitchDo={'+': ()=>{const[p1,p2]=[points.pop(),points.pop()]points.push(p2)points.push(p1)points.push(p1+p2)},'D': ()=>points.push(points.top()*2),'C': ()=>total-=points.pop(),}constdefaultDo=(e)=>points.push(parseInt(e))constdispatch=(e)=>{if(einswitchDo)switchDo[e]()elsedefaultDo(e)}for(consteofops){// do what should dodispatch(e)// increase total if we shouldif(e!=='C')total+=points.top()}returntotal};
The text was updated successfully, but these errors were encountered:
题:
你现在是棒球比赛记录员。
给定一个字符串列表,每个字符串可以是以下四种类型之一:
1.整数(一轮的得分):直接表示您在本轮中获得的积分数。
2. "+"(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。
3. "D"(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。
4. "C"(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应该被移除。
每一轮的操作都是永久性的,可能会对前一轮和后一轮产生影响。
你需要返回你在所有回合中得分的总和。
示例 1:
示例 2:
注意:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/baseball-game
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解:按照题意手搓几个回合,应该能感受到「栈」的感觉。
此题的「循环不变式」就是在循环过程中,对「栈」进行正确操作,同时正确更新此时的总分。
The text was updated successfully, but these errors were encountered: