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 ...
随机推荐
- 【ESP32 IDF】用RMT控制 WS2812 彩色灯带
在上一篇中,老周用 .NET Nano Framework 给大伙伴们演示了 WS2812 灯带的控制,包括用 SPI 和 红外RMT 的方式.利用 RMT 是一个很机灵的方案,不过,可能很多大伙伴对 ...
- vue 商品sku添加,笛卡尔算法,商品添加。动态生成table,table添加值后 再生成的table 不改变table之前输入的值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 欢迎 Llama 3:Meta 的新一代开源大语言模型
介绍 Meta 公司的 Llama 3 是开放获取的 Llama 系列的最新版本,现已在 Hugging Face 平台发布.看到 Meta 持续致力于开放 AI 领域的发展令人振奋,我们也非常高兴地 ...
- 新零售标杆 SKG 全面拥抱 Serverless,实现敏捷交付
简介: SKG CTO 王焱:以前需要 60 个人干的活,用 SAE 和大禹后 15 个人就可以! 作者:陈列昂.昕辰.龙琛.黛忻 项目背景 SKG 公司是一家专注于高端健康产品的研发.设计与制造 ...
- [FAQ] Cordova 模拟器中不能访问域名, 未联网 ?
首先保证电脑已联网,然后打开模拟器的浏览器输入常用网址,看看是否能够联网. 如果访问失败,在本机中在 cmd 中 ping www.baidu.com 获得百度的ip地址,然后在浏览器中输入 http ...
- 本周ddl(4.1-4.5)
本周ddl(4.1-4.5) cs61a首先完成2.23之前的任务 cs61a完成3.1之前的学习 cs61a完成3.8任务并且ants完成阶段1 csapp的bomblab 记录南京5个景点及其周边 ...
- 🎉号外号外!OpenTiny 将在HDC华为开发者大会正式发布!
华为开发者大会2023(HDC.Cloud 2023)将于7月7日-9日在东莞拉开帷幕,本届大会以"每一个开发者都了不起"为主题,同时在全球10余个国家以及中国30多个城市设有分会 ...
- 魔方OA 数据字典
https://gitee.com/mojocube/mc-oa/blob/master/Data/%E6%95%B0%E6%8D%AE%E5%BA%93%E8%84%9A%E6%9C%AC.sql ...
- 【内存优化】Oracle 的SGA与Linux的shmall和shmmax的关联
查看linux下的Oracle共享内存段 [oracle@oradb ~]$ ipcs -m ------ Shared Memory Segments -------- key shmid owne ...
- Golang csv操作
目录 csv读写 追加写入 追加写入封装 csv读写 封装成工具包 package utils import ( "encoding/csv" "fmt" &q ...