题目:

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. leetcode插件问题

    1.使用一段时间后,提交答案一直返回undefind 原因为插件缓存token有效期已过,需要重新登录 2. 重新登录

  2. 【力扣精选】Oracle SQL 176. 第二高的薪水

    [力扣精选]Oracle SQL 176. 第二高的薪水 这道题很适合用来作为窗口函数的入门使用练习 链接如下: https://leetcode.cn/problems/second-highest ...

  3. 力扣183(MySQL)-从不订购的客户(简单)

    题目: 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: Orders 表:  解题思路: 需要查询出没 ...

  4. 一个开发者自述:我是如何设计针对冷热读写场景的 RocketMQ 存储系统

    简介: 文章中的很多知识点,都是通过云原生编程挑战赛学到的,在一些问题在表述方式.甚至理解上都可能存在一些问题,甚至会有一些谬论:敢于尝试就会犯错,有犯错才会有成长,欢迎各位大佬不舍赐教,多多指正,让 ...

  5. 超大福利 | 这款免费 Java 在线诊断利器,不用真的会后悔!

    线上系统为何经常出错?数据库为何屡遭黑手?业务调用为何频频失败?连环异常堆栈案,究竟是哪次调用所为? 数百台服务器意外雪崩背后又隐藏着什么?是软件的扭曲还是硬件的沦丧? 走进科学带你了解 Arthas ...

  6. 最佳实践丨三种典型场景下的云上虚拟IDC(私有池)选购指南

    ​简介:业务上云常态化,业务在云上资源的选购.弹性交付.自助化成为大趋势.不同行业的不同客户,业务发展阶段不一样,云上资源的成本投入在业务整体成本占比也不一样,最小化成本投入.最大化业务收益始终是不同 ...

  7. 如何在golang代码里面解析容器镜像

    ​简介:容器镜像在我们日常的开发工作中占据着极其重要的位置.通常情况下我们是将应用程序打包到容器镜像并上传到镜像仓库中,在生产环境将其拉取下来.然后用 docker/containerd 等容器运行时 ...

  8. 基于Delta lake、Hudi格式的湖仓一体方案

    ​简介: Delta Lake 和 Hudi 是流行的开放格式的存储层,为数据湖同时提供流式和批处理的操作,这允许我们在数据湖上直接运行 BI 等应用,让数据分析师可以即时查询新的实时数据,从而对您的 ...

  9. [FAQ] 清理 Docker 环境长期构建占用磁盘空间过大问题

      $ docker system df 长时间积累多次运行 docker 构建过程,Build Cache 缓存几乎占据了硬盘 1/3 的容量. $ docker system  prune 此命令 ...

  10. [Caddy2] cloudflare, acme: cleaning up failed: no memory of presenting a DNS record

    使用 cloudflare 做为 DNS 之后,使用 Caddy 申请 Lets Encrypt 证书. 有时在日志里会发现一系列的提示信息: acme: use dns-01 solver acme ...