当前位置:网站首页>682. 棒球比赛
682. 棒球比赛
2022-06-11 08:55:00 【拽拽就是我】
leetcode力扣刷题打卡
题目:682. 棒球比赛
描述:你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。
比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作,ops 遵循下述规则:
整数 x - 表示本回合新获得分数 x
“+” - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。
“D” - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。
“C” - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。
请你返回记录中所有得分的总和。
解题思路
1、进进出出的操作那肯定是栈了;
2、+的时候,为了得到前两次的得分,需要先弹出前一次的得分,拿到前第二次的得分后,再把前一次的得分放回去;
3、stoi函数(字符串转整型)也太好用了;
原代码##
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int>s;
int ans = 0;
for (int i = 0; i < ops.size(); ++i) {
char ch = ops[i][0];
if (ch == 'C') {
ans -= s.top();
s.pop();
} else if (ch == 'D') {
int temp = s.top() * 2;
s.push(temp);
ans += s.top();
} else if (ch == '+'){
int pre1 = s.top();
s.pop();
int pre2 = s.top();
s.push(pre1);
int temp = pre1 + pre2;
s.push(temp);
ans += s.top();
} else {
int temp = stoi(ops[i]);
s.push(temp);
ans += temp;
}
}
return ans;
}
};
边栏推荐
- Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘
- En45545-2 R26 vertical combustion test introduction
- win10家庭版如何连接远程桌面
- MySQL & Oracle database capacity query
- (二)从架构设计角度分析AAC源码-我的LiveData
- 【C语言-函数栈帧】从反汇编的角度,剖析函数调用全流程
- Which Apple devices support this system update? See if your old apple device supports the latest system
- Matlab learning 7- linear smoothing filtering of image processing
- SAP ODATA 开发教程
- EN 45545-2:2020 T11烟毒性检测
猜你喜欢

Matlab r2022a installation tutorial

Sword finger offer 62 The last remaining number in the circle

MySQL核心点笔记

一些学习记录i=

Sword finger offer 21 Adjust array order so that odd numbers precede even numbers

进程间的通信

马志强:语音识别技术研究进展和应用落地分享丨RTC Dev Meetup

知识图谱入门之---yedda标注

What is concurrent search set? Are you still worried about it? In fact, it is a problem of connected graph, which is not so difficult to understand

(1) AAC opening - lifecycle, livedata, ViewModel and source code analysis skills of core component principles (Reprint)
随机推荐
Complete ES6 questions
面试官问了四个问题,总结了4个经验
c语言打印心形
Android interview transcript (carefully sorted out)
CodeTop - 排序奇升偶降链表
Wood board ISO 5660-1 heat release rate mapping test
M1 芯片指南:M1、M1 Pro、M1 Max 和 M1 Ultra
Sword finger offer 62 The last remaining number in the circle
二、渲染 Camera 的数据
Android 面试笔录(精心整理篇)
PHP uploading large files for more than 40 seconds server 500
SAP abap内表分类与增删改查操作
win10家庭版如何连接远程桌面
SAP material master data archiving
领导让我重写测试代码,我也要照办嘛?
剑指 Offer 51. 数组中的逆序对
Using docker compose to build redis5 cluster
哪些Apple设备支持这次系统更新?来看你的旧Apple设备支持最新系统吗
K8s application (IV) - build a redis5 cluster (for direct external access)
What software is required to process raw format images?