前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode-682-Baseball Game

leetcode-682-Baseball Game

作者头像
chenjx85
发布2018-05-22 16:35:16
3880
发布2018-05-22 16:35:16
举报

题目描述:

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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
    int calPoints(vector<string>& ops) 
    {
        int sum=0;
        stack<int>cur;//存储每一次的此轮得分
        int s1=ops.size();
        int result;//中间变量
        int t;//中间变量
        int t1,t2;//中间变量
        for(int i=0;i<s1;i++)
        {
            if(ops[i]=="D")
            {
                t=2*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][0]=='-')//如果是负数
                {
                    result=0;
                    for(int j=1;j<ops[i].size();j++)
                    {
                        result=result*10+ops[i][j]-'0';
                    }
                    result*=-1;
                }
                else 
                {        //如果是正数
                    result=0;
                    for(int j=0;j<ops[i].size();j++)
                    {
                        result=result*10+ops[i][j]-'0';
                    }
                }
                cur.push(result);
                sum+=result;
            }
        }
        return sum;
    }

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 要完成的函数:
  • 说明:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档