LeetCode 682. Baseball Game 棒球比赛(C++/Java)
题目:
You're now a baseball game point recorder.
Given a list of strings, each string can be one of the 4 following types:
Integer(one round's score): Directly represents the number of points you get in this round."+"(one round's score): Represents that the points you get in this round are the sum of the last twovalidround's points."D"(one round's score): Represents that the points you get in this round are the doubled data of the lastvalidround's points."C"(an operation, which isn't a round's score): Represents the lastvalidround'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)的更多相关文章
- [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 ...
- Leetcode682.Baseball Game棒球比赛
你现在是棒球比赛记录员. 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数. 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 ...
- 682. Baseball Game 棒球游戏 按字母处理
[抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...
- Java实现 LeetCode 682 棒球比赛(暴力)
682. 棒球比赛 你现在是棒球比赛记录员. 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数. 2. "+"(一轮的 ...
- LeetCode:棒球比赛【682】
LeetCode:棒球比赛[682] 题目描述 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. " ...
- 【Leetcode】【简单】【682棒球比赛】【JavaScript】
题目 682. 棒球比赛 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. "+"(一轮的 ...
- [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 ...
- LeetCode高频题目(100)汇总-Java实现
LeetCode高频题目(100)汇总-Java实现 LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...
- 【Leetcode_easy】682. Baseball Game
problem 682. Baseball Game solution: 没想到使用vector! class Solution { public: int calPoints(vector<s ...
随机推荐
- EasyNLP带你玩转CLIP图文检索
简介: 本文简要介绍CLIP的技术解读,以及如何在EasyNLP框架中玩转CLIP模型. 作者:熊兮.章捷.岑鸣.临在 导读 随着自媒体的不断发展,多种模态数据例如图像.文本.语音.视频等不断增长,创 ...
- 什么是好的错误消息? 讨论一下Java系统中的错误码设计
简介:一个好的Error Message主要包含三个部分:Context: 什么导致了错误?发生错误的时候代码想做什么?The error itself: 到底是什么导致了失败?具体的原因和当时的数据 ...
- [FAQ] 适用于 macOS / Arm64 (M1/M2) 的 VisualBox
使用与 Windows.Linux.macOS 的x86架构的一般在下面地址中下载: Download VisualBox:https://www.virtualbox.org/wiki/Down ...
- Ansible的yaml文件
ansible提供的脚本,遵循规范yaml(一般用于写配置文件) 可用于配制文件的语言:yaml.xml.json - 冒号后面必须有空格 - 横线后面必须要空格 - 严格保持对齐 - 等号前面不能有 ...
- SonarQube+Maven+SonarQube Scanner
1.SonarQube简介 官方网站地址:https://www.sonarqube.org/ SonrQube是一个开源的代码质量管理系统,用于检测代码中的错误,漏洞和代码规范.它可以以现有的Git ...
- C#的基于.net framework的Dll模块编程(四) - 编程手把手系列文章
这次继续这个系列的介绍: 一.命名空间的起名: 对于C#来说,一般命名空间的建议是:公司名(或个人名称).产品名.分类名,比如我这边是用的这个:Lzhdim.LPF.Helper,意思是个人名Lzhd ...
- 应用zabbix的实时导出(real-time export)功能
说明 zabbix作为监控软件,有时也会需要获取历史数据作进一步的分析,通常可以采用3种办法: 通过zabbix API定期获取(通过web) 通过后端数据库定期读取(通过db) 应用实时导出功能配合 ...
- 一:大数据架构回顾-Lambda架构
"我们正在从IT时代走向DT时代(数据时代).IT和DT之间,不仅仅是技术的变革,更是思想意识的变革,IT主要是为自我服务,用来更好地自我控制和管理,DT则是激活生产力,让别人活得比你好&q ...
- 深入学习和理解Django视图层:处理请求与响应
title: 深入学习和理解Django视图层:处理请求与响应 date: 2024/5/4 17:47:55 updated: 2024/5/4 17:47:55 categories: 后端开发 ...
- Jetbrains系列产品最新激活方法[持续更新]
Jetbrains系列产品最新激活方法[持续更新] 2021.3.4系列激活 方法一: 2021.3.4参考文章: https://www.exception.site/essay/how-to-fr ...