题目:

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.

分析:

记录棒球比赛的比分,有如下几个规则:

如果是数字的话,该轮等分就是这个数字。

如果是D,该轮得分等于上一轮得分的双倍。

如果是+,该轮的分等于前两轮得分之和。

如果是C,前一轮得分错误,应该删去前一轮得分。

知道所有规则,题目就简单多了,用一个数组来保存每一轮的得分,依据所给的操作,对数组进行相应的操作即可,最后返回数组的和。

程序:

C++

class Solution {
public:
int calPoints(vector<string>& ops) {
vector<int> res;
for(string s:ops){
int n = res.size();
if(s == "+"){
res.push_back(res[n-1] + res[n-2]);
}else if(s == "D"){
res.push_back(2 * res[n-1]);
}else if(s == "C"){
res.pop_back();
}else{
res.push_back(atoi(s.c_str()));
}
}
int sum = 0;
for(int i:res){
sum += i;
}
return sum;
}
};

Java

class Solution {
public int calPoints(String[] ops) {
List<Integer> list = new ArrayList<>();
for(String s:ops){
int n = list.size();
if(s.equals("+")){
list.add(list.get(n-1) + list.get(n-2));
}else if(s.equals("D")){
list.add(2 * (int)list.get(n-1));
}else if(s.equals("C")){
list.remove(n-1);
}else{
list.add(Integer.parseInt(s));
}
}
int sum = 0;
for(int i:list)
sum += i;
return sum;
}
}

LeetCode 682. Baseball Game 棒球比赛(C++/Java)的更多相关文章

  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. Leetcode682.Baseball Game棒球比赛

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

  3. LeetCode 682 Baseball Game 解题报告

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

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

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

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

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

  6. LeetCode:棒球比赛【682】

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

  7. 【Leetcode】【简单】【682棒球比赛】【JavaScript】

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

  8. [Swift]LeetCode682. 棒球比赛 | Baseball Game

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

  9. LeetCode高频题目(100)汇总-Java实现

    LeetCode高频题目(100)汇总-Java实现       LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...

  10. 【Leetcode_easy】682. Baseball Game

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

随机推荐

  1. NOI 2024省选OIFC模拟21 蒲巴巴 超繁做法

    题目描述 一年一度的 PuBaBa 杯开始了! 今年的 PuBaBa 杯总共有 \(n\) 个选手来参加,编号分别为 \(1,2,\cdots,n\),他们的水平按编号依次递增,所以他们过的题目数量单 ...

  2. 【笔记】go语言--(Slice)切片的概念

    go--(Slice)切片的概念 //切片是什么,定义一个arr,定义一个s为arr中的2到6,这个s就是一个切片 arr := [...]int{0,1,2,3,4,5,6,7} s := arr[ ...

  3. [FE] yarn, npm 切换镜像源

    yarn 设置命令如下,会修改 ~/.yarnrc 内容. $ yarn config set registry https://registry.yarnpkg.com npm 设置命令如下,会修改 ...

  4. 使用 Silk.NET 创建 OpenGL 空窗口项目例子

    本文告诉大家如何使用 Silk.NET 创建 OpenGL 空窗口项目.在 dotnet 基金会下,开源维护 Silk.NET 仓库,此仓库提供了渲染相关的封装逻辑,包括 DX 和 OpenGL 等等 ...

  5. MSIL 静态类在 IL 定义上和非静态类的差别

    本文来聊聊 MSIL 的基础知识,给一个 C# 的类标记了 static 之后和标记 static 之前,生成这个类的 IL 代码有什么不同 如以下的代码是一个默认的控制台程序 class Progr ...

  6. vue-公共组件的注册

    注册公共组件,在每个需要的页面直接输入文件名(<g-table/>)即可引用该组件 步骤: 1.新建components/global文件夹,以及components/global/g-t ...

  7. Java的Stream流的分页,Stream的skip和limit实现分页

    1.工具类 package com.cc.testproject.utils; import com.github.pagehelper.PageInfo; import org.springfram ...

  8. SAP集成技术(二)接口库

    目前还没有一个集中的地方能找到全部SAP接口,它们分散在几个不同的地方. 本文链接:https://www.cnblogs.com/hhelibeb/p/17843509.html 内容部分来自< ...

  9. Linux使用宝塔面板发布.NetCore3.1

    1,安装宝塔面板 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0 ...

  10. PVT:特征金字塔在Vision Transormer的首次应用,又快又好 | ICCV 2021

    论文设计了用于密集预测任务的纯Transformer主干网络PVT,包含渐进收缩的特征金字塔结构和spatial-reduction attention层,能够在有限的计算资源和内存资源下获得高分辨率 ...