题目:

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. 力扣196(MySQL)-删除重复的电子邮箱(简单)

    题目: 表: Person 编写一个 SQL 删除语句来 删除 所有重复的电子邮件,只保留一个id最小的唯一电子邮件. 以 任意顺序 返回结果表. (注意: 仅需要写删除语句,将自动对剩余结果进行查询 ...

  2. 力扣1620(java&python)-网络信号最好的坐标(中等)

    题目: 给你一个数组 towers 和一个整数 radius . 数组  towers  中包含一些网络信号塔,其中 towers[i] = [xi, yi, qi] 表示第 i 个网络信号塔的坐标是 ...

  3. Web3开发者技术选型:前端视角(next.js)

    引言 在现代Web开发的世界中,Web3技术的兴起为前端开发者开辟了新的可能性.Web3技术主要指的是建立在区块链基础上的分布式网络,使用户能够通过智能合约和去中心化应用(DApps)直接交互,而无需 ...

  4. 五分钟学会使用 go modules(含在家办公使用技巧)

    导读:go modules 是 golang 1.11 新加的特性.如今 1.13 都已经发布了第 7 个小版本了,几乎所有大项目均已开始使用,这自然也包括 Kubernetes 生态中的众多项目.笔 ...

  5. 配置审计(Config)配合开启OSS防盗链功能

    简介: 本文作者:紫极zj 本文将主要介绍利用[配置审计]功能,如何快速发现企业上云过程中,针对未配置防盗链的 OSS Bucket 定位及修复案例. 前言 配置审计(Config)将您分散在各地域的 ...

  6. WPF 通过 RawInput 获取触摸消息

    触摸在 Windows 下属于比较特殊的输入,不同于键盘和鼠标,键盘和鼠标可以通过全局 Hook 的方式获取到鼠标和键盘的输入消息.而触摸则没有直接的 Hook 的方法.如果期望自己的应用,可以在没有 ...

  7. WPF 加载诡异的字体无法布局

    如果在系统里面存在诡异的字体,同时自己的 WPF 中有一个控件尝试使用这个字体放在界面中,那么将会在界面布局过程炸了,整个控件或者整个界面布局都无法继续 本文本来是由吕水大大发布的,但是他没空写,于是 ...

  8. ITIL现有版本之间的区别

    时代在变化,运维管理理论也在不断演进升级,不断学习是运维人的良好品质:虽然人有的时候会懈怠,理论学习的道路也较单调乏味,但终究还是要跟上时代的步调才能适应新的变化

  9. DNS(1) -- DNS服务及dns资源类型

    目录 1.1 DNS服务概述 1.2 DNS域名结构 1.3 DNS解析原理 1.3.1 DNS查询类型 1.3.2 解析答案 1.4 DNS资源记录类型 1.1 DNS服务概述 DNS(Domain ...

  10. WEB服务与NGINX(14)-NGINX的压缩功能

    1. nginx压缩功能 nginx支持对指定类型的文件进行压缩后再回传给客户端,而且压缩可以设置压缩比,压缩后的文件会明显变小,有助于降低出口带宽的利用率,但是会占用一定的CPU资源. nginx实 ...