题目描述:

You're now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round's score): Directly represents the number of points you get in this round.
  2. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
  3. "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
  4. "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.

Each round's operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

Example 1:

Input: ["5","2","C","D","+"]
Output: 30
Explanation:
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get 2 points. The sum is: 7.
Operation 1: The round 2's data was invalid. The sum is: 5.
Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.

Example 2:

Input: ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get -2 points. The sum is: 3.
Round 3: You could get 4 points. The sum is: 7.
Operation 1: The round 3's data is invalid. The sum is: 3.
Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
Round 5: You could get 9 points. The sum is: 8.
Round 6: You could get -4 + 9 = 5 points. The sum is 13.
Round 7: You could get 9 + 5 = 14 points. The sum is 27.

Note:

  • The size of the input list will be between 1 and 1000.
  • Every integer represented in the list will be between -30000 and 30000.

要完成的函数:

int calPoints(vector<string>& ops)

说明:

1、翻译一下题意,你是一个棒球记分员,要你输出最后的总分。

给定一个vector,里面装着每一轮的操作,操作是string类型的。

如果操作是D,也就是当前轮次的得分是上一轮(有效)的得分乘以2。

如果操作是+,也就是当前轮次的得分是上一轮(有效)和再上一轮(有效)的得分之和。

如果操作是C,也就是当前轮次没有得分,而且上一轮得分宣告无效。

如果操作是一个整数,该整数也就是当前轮次的得分。

以一个例子来走一趟整个流程,["5","-2","4","C","D","9","+","+"]

第一个数,此轮得分5,总分5

第二个数,此轮得分-2,总分3

第三个数,此轮得分4,总分7

第四个数,C,上一轮得分4无效,总分3

第五个数,D,上一轮(有效)得分为-2,所以此轮次得分-4,总分-1

第六个数,此轮得分9,总分8

第七个数,+,上一轮(有效)得分9,再上一轮(有效)得分-4,所以此轮得分5,总分13

第八个数,+,上一轮(有效)得分5,再上一轮(有效)得分9,所以此轮得分14,总分27

逻辑清晰,我们构造如下代码:

    int calPoints(vector<string>& ops)
{
int sum=;
stack<int>cur;//存储每一次的此轮得分
int s1=ops.size();
int result;//中间变量
int t;//中间变量
int t1,t2;//中间变量
for(int i=;i<s1;i++)
{
if(ops[i]=="D")
{
t=*cur.top();//上一轮得分*2
sum+=t;
cur.push(t);
}
else if(ops[i]=="+")
{
t1=cur.top();//上一轮得分
cur.pop();
t2=cur.top();//再上一轮得分
t=t1+t2;
sum+=t;
cur.push(t1);
cur.push(t);
}
else if(ops[i]=="C")
{
sum-=cur.top();//减去上一轮得分
cur.pop();//宣告无效,移出cur
}
else
{
if(ops[i][]=='-')//如果是负数
{
result=;
for(int j=;j<ops[i].size();j++)
{
result=result*+ops[i][j]-'';
}
result*=-;
}
else
{        //如果是正数
result=;
for(int j=;j<ops[i].size();j++)
{
result=result*+ops[i][j]-'';
}
}
cur.push(result);
sum+=result;
}
}
return sum;
}

笔者觉得上述代码没有多少浪费的地方。实测效果也还可以,6ms,beats 98.60% of cpp submissions。

如果同学们觉得还有可以改进的地方,欢迎在评论区中留言!

leetcode-682-Baseball Game的更多相关文章

  1. [LeetCode] 682. Baseball Game 棒球游戏

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

  2. LeetCode 682 Baseball Game 解题报告

    题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...

  3. 【Leetcode_easy】682. Baseball Game

    problem 682. Baseball Game solution: 没想到使用vector! class Solution { public: int calPoints(vector<s ...

  4. 【LeetCode】682. Baseball Game 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈模拟 日期 题目地址:https://leet ...

  5. [LeetCode&Python] Problem 682. Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

  6. 682. Baseball Game 棒球游戏 按字母处理

    [抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...

  7. Java实现 LeetCode 682 棒球比赛(暴力)

    682. 棒球比赛 你现在是棒球比赛记录员. 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数. 2. "+"(一轮的 ...

  8. 682. Baseball Game

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 682. Baseball Game (5月28日)

    解答(打败98.60%) class Solution { public: int calPoints(vector<string>& ops) { vector<int&g ...

  10. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. 修改字符集AL32UTF8修改为ZHS16GBK详解

    登陆sqlplus,在命令行输入 sqlplus sys/sys as sysdba;//登陆sqlplus SQL>SHUTDOWN IMMEDIATE; SQL>STARTUP MOU ...

  2. 无法访问windows安装服务。发生这种情况的可能是您在安全模式下运行windows,或是没有正确安装windows安装,。请与技术支持人员联系以获得帮助。

    解决办法: 1.命令提示符下输入:msiexec/regserver 2.在“管理工具”→“服务”中启动windows Installer 程序员的基础教程:菜鸟程序员

  3. 从零开始安装hue(原创-转载注明出处)

    hue安装需要从github上面下载源码,进行编译安装.github上面给出的安装教程很简单 然而实际上在安装的过程中遇到了无数个坑,下面开始真正意义上的从零开始安装hue. 安装环境: centOS ...

  4. [Excel]鼠标右键菜单没有新建Word、Excel、PPT怎么办?

    很多朋友在安装好Office(2010或2013等)之后,发现右键新建中没有Word.Excel.PowerPoint等项,但是自己的Office却明明安装好了.这个时候该怎么办呢?这里,本文为大家提 ...

  5. Oracle 11g 重建EM需要删除的对象

    因为需求需要重建EM,重建时因为某些错误被迫停止,比如对象已存在.用户已经存在等,最终找出了创建必备的条件: 1.环境变量(Oracle和Grid在同一个用户下安装) ORACLE_HOME 要设为D ...

  6. Session分布式共享 = Session + Redis + Nginx(转)

    出处:http://www.cnblogs.com/newP/p/6518918.html 一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我 ...

  7. (4)WePHP 模板引入CSS js

    模板有两个定义了两个常量 父类已经定义好了 //模板常量 $dirStr=dirname($_SERVER['SCRIPT_NAME']); $dirStr=$dirStr=='\\'?NULL:$d ...

  8. URAL 1430. Crime and Punishment(数论)

    题目链接 题意 :给你a,b,n,让你找出两个数x,y,使得n-(a*x+b*y)最小. 思路 : 分大小做,然后枚举a的倍数 #include <stdio.h> #include &l ...

  9. CALayer, CoreGraphics与CABasicAnimation介绍

    今天我们来看一下CALayer.CoreGraphics和CABasicAnimation.这些东西在处理界面绘制.动画效果上非常有用. 本篇博文就讲介绍CALayer的基本概念,使用CoreGrap ...

  10. Eclipse的Debug调试技巧大全

    转载 原文链接:https://blog.csdn.net/u011781521/article/details/55000066 收藏方便以后查看. 19:18:10 2018-12-29