当前位置:网站首页>682. baseball game
682. baseball game
2022-06-11 08:56:00 【Drag is me】
leetcode Force button to brush questions and punch in
subject :682. baseball game
describe : You are now a recorder of a special baseball game . The game consists of several rounds , The score of the past few rounds may affect the score of the next few rounds .
At the beginning of the game , The record is blank . You get a list of strings that record operations ops, among ops[i] It's the number one you need to record i Actions ,ops Follow these rules :
Integers x - This is a new round x
“+” - The new score of this round is the sum of the previous two scores . Topic data ensures that there are always two valid scores in front of this operation .
“D” - The new score in this round is twice that of the previous one . The title data ensures that there is always a valid score before recording this operation .
“C” - The previous score is invalid , Remove it from the record . The title data ensures that there is always a valid score before recording this operation .
Please return the sum of all the scores in the record .
Their thinking
1、 In and out operations must be stack ;
2、+ When , In order to get the first two scores , You need to pop up the previous score first , After getting the second score before , Put back the previous score ;
3、stoi function ( String to integer ) It's also very easy to use ;
Source code ##
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;
}
};
边栏推荐
- 九九乘法表
- En45545-2 R26 vertical combustion test introduction
- SAP ABAP field symbol
- The interviewer asked four questions and summed up four experiences
- Android interview transcript (carefully sorted out)
- 206. reverse linked list
- EN45545-2 R26垂直燃烧测试介绍
- 光伏板怎么申请ASTM E108阻燃测试?
- EN 45545 R24氧指数测试方法解析
- 硅树脂油漆申请美国标准UL 790 Class A 合适吗?
猜你喜欢

Intelligent control theory question bank

AS 3744.1标准中提及ISO8191测试,两者测试一样吗?

Matlab学习9-图像处理之非线性锐化滤波

使用express+mysql创建一个基于nodejs的后台服务

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

矩阵求逆操作的复杂度分析(逆矩阵的复杂度分析)

M1 芯片指南:M1、M1 Pro、M1 Max 和 M1 Ultra

Matlab learning 8- linear and nonlinear sharpening filtering and nonlinear smoothing filtering of image processing

What software is required to process raw format images?

处理RAW格式的图像,需要什么软件?
随机推荐
Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?
窗帘做EN 1101易燃性测试过程是怎么样的?
What if the copied code format is confused?
面试官问了四个问题,总结了4个经验
Matlab学习9-图像处理之非线性锐化滤波
Question d'entrevue 02.02. Renvoie l'avant - dernier noeud K
端口占用问题,10000端口
EN45545-2 R26垂直燃烧测试介绍
MySQL & Oracle database capacity query
Codetop - sort odd ascending even descending linked list
206. reverse linked list
SAP ABAP field symbol
Livedata and stateflow, which should I use?
Port occupancy problem, 10000 ports
Analysis of EN 45545 R24 oxygen index test method
完整的ES6面试题
php 上传大文件 超过40秒 服务器500
GCC AVR (ATMEL studio+ AVR studio) how to define the structure array in the program memory (flash) space and read it
Codeworks round 723 (Div. 2)
Vagrant mounting pit