LeetCode682. Baseball Game

2023-12-29 07:04
文章标签 game baseball leetcode682

本文主要是介绍LeetCode682. Baseball Game,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一、题目
    • 二、题解

一、题目

You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.

You are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following:

An integer x.
Record a new score of x.
‘+’.
Record a new score that is the sum of the previous two scores.
‘D’.
Record a new score that is the double of the previous score.
‘C’.
Invalidate the previous score, removing it from the record.
Return the sum of all the scores on the record after applying all the operations.

The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.

Example 1:

Input: ops = [“5”,“2”,“C”,“D”,“+”]
Output: 30
Explanation:
“5” - Add 5 to the record, record is now [5].
“2” - Add 2 to the record, record is now [5, 2].
“C” - Invalidate and remove the previous score, record is now [5].
“D” - Add 2 * 5 = 10 to the record, record is now [5, 10].
“+” - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.
Example 2:

Input: ops = [“5”,“-2”,“4”,“C”,“D”,“9”,“+”,“+”]
Output: 27
Explanation:
“5” - Add 5 to the record, record is now [5].
“-2” - Add -2 to the record, record is now [5, -2].
“4” - Add 4 to the record, record is now [5, -2, 4].
“C” - Invalidate and remove the previous score, record is now [5, -2].
“D” - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
“9” - Add 9 to the record, record is now [5, -2, -4, 9].
“+” - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
“+” - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
Example 3:

Input: ops = [“1”,“C”]
Output: 0
Explanation:
“1” - Add 1 to the record, record is now [1].
“C” - Invalidate and remove the previous score, record is now [].
Since the record is empty, the total sum is 0.

Constraints:

1 <= operations.length <= 1000
operations[i] is “C”, “D”, “+”, or a string representing an integer in the range [-3 * 104, 3 * 104].
For operation “+”, there will always be at least two previous scores on the record.
For operations “C” and “D”, there will always be at least one previous score on the record.

二、题解

class Solution {
public:int calPoints(vector<string>& operations) {vector<int> tmp;for(auto str:operations){if(str[0] != '-' && !isdigit(str[0])){char c = str[0];int n = tmp.size();if(c == '+') tmp.push_back(tmp[n-1] + tmp[n-2]);else if(c == 'D') tmp.push_back(tmp[n-1] * 2);else if(c == 'C') tmp.pop_back();}else tmp.push_back(stoi(str));}int res = 0;for(auto x:tmp){res += x;}return res;}
};

这篇关于LeetCode682. Baseball Game的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/548710

相关文章

fzu 2275 Game KMP

Problem 2275 Game Time Limit: 1000 mSec    Memory Limit : 262144 KB  Problem Description Alice and Bob is playing a game. Each of them has a number. Alice’s number is A, and Bob’s number i

10400 -Game Show Math

这道题的话利用了暴力深搜,尽管给了20S,但是这样还会超时,所以就需要利用回溯进行减枝,因为是DFS,所以用一个数组vis[i][j]记录是否在状态i时候取到过j值,如果取到过的话,那么直接回溯(往后搜索已经没有意义了,之前到达这个状态的时候是无法得到结果的) 还有需要注意的地方就是题目的要求,每一步的结构都在(-32000,32000)之间,所以需要一步判断,如果在这个范围外直接回溯 最后一

【POJ】1733 Parity game 并查集

传送门:【POJ】1733 Parity game 题目大意:给你一个长度为n的01序列,再给你m句话,每句话是一个区间【L,R】,告诉你区间【L,R】中1的个数,现在你的任务是找到从第几句话开始说的和前面矛盾,出现第一次假话的时候前面有多少是真话。 题目分析:一开始看几乎没思路啊。后来没办法了,只能跑别人的博客去看看了。。。一看到说把一个区间【L,R】拆成两个区间【0,L-1】,

【HDU】5426 Rikka with Game【DP】

题目链接:【HDU】5426 Rikka with Game #include <bits/stdc++.h>using namespace std ;typedef long long LL ;#define clr( a , x ) memset ( a , x , sizeof a )const int MAXN = 100005 ;const int MAXE = 200005 ;

LeetCode 45 Jump Game II

题意: 给出一个步长数组nums,如果一个人站在i这个点上那么他可以向右最多走nums[i]步,求从左端点走到右端点的最少步数。 思路: 如果点x可以用dp[x]步到达,那么[ x + 1, x + nums[x] ]区间内的点都可以用dp[x] + 1步到达。 利用这个想法,可以O(n)的求出走一步可以到达哪些位置,走两步可以到达哪些位置,以此类推。 代码: clas

【论文笔记】Multi-Task Learning as a Bargaining Game

Abstract 本文将多任务学习中的梯度组合步骤视为一种讨价还价式博弈(bargaining game),通过游戏,各个任务协商出共识梯度更新方向。 在一定条件下,这种问题具有唯一解(Nash Bargaining Solution),可以作为多任务学习中的一种原则方法。 本文提出Nash-MTL,推导了其收敛性的理论保证。 1 Introduction 大部分MTL优化算法遵循一个通用方

android-Intent,Injector,Template,Adapter,Validation,Gesture,Game,Game Engine,Bluetooth...

Intent Intent PhotoPicker 图片选择 & 图片预览https://github.com/donglua/PhotoPicker Injector AndroidAnnotations Fast Android Development. Easy maintainance. https://github.com/excilys/androidannotations

HDU5515 Game of Flying Circus(二分)

题意:题解有翻译,然后自己拦截对手时候可以任意走,当然是直线最快啦 题解:http://www.cnblogs.com/qscqesze/p/4931912.html #include<bits/stdc++.h>using namespace std;#define LL long long#define pb push_back#define X first#define Y

hdu 1846 Brave Game 巴什博奕

Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫《勇敢者的游戏》(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻。  今天,大家选择上机考试,就是一种勇敢(brave)的选择;这个短学期,我们讲的是博弈(game)专题;所以,大家现在玩的也是“勇敢者的游戏”,这也是我命名这个题目的原因。  当然,除了

hdu 1524 A Chess Game 博弈论

题意: 两个人在一个有向五环图上面走棋子,每次只能走一步,最后谁 * 没有棋子可走就败,然后棋子可以重叠,并且有n个棋子。要求判断 * 先手的胜负。 纠结了好长时间一直在想为什么sg函数要呢么定义然后看了各种博客但是只是讲了,定义的内容却很少有讲为什么的。。。。 Description Let's design a new chess game. There are N