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;}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/584764.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android 13 默认关闭 快速打开相机

介绍 在设置菜单的手势界面里&#xff0c;快速打开相机是默认开启的&#xff0c;此功能当开启时连续点击两次电源键会打开相机&#xff0c;现在客户需要默认关闭。 效果展示 修改 这里一开始想到的就是配置文件&#xff0c;在路径下果然找到了,从注释中看使我们需要的&#x…

亚马逊云科技Amazon MSK基于S3云服务器实现导出导入、备份还原、迁移方案

亚马逊云科技Amazon MSK是Amazon云平台提供的托管Kafka服务。在系统升级或迁移时&#xff0c;用户常常需要将一个Amazon MSK集群中的数据导出&#xff08;备份&#xff09;&#xff0c;然后在新集群或另一个集群中再将数据导入&#xff08;还原&#xff09;。通常&#xff0c;K…

flink generic log-based incremental checkpoints 设计

背景 flink 在1.15版本后开始提供generic log-based incremental checkpoints的检查点方案&#xff0c;目的在于减少checkpoint的耗时&#xff0c;尽量缩短端到端的数据处理延迟&#xff0c;本文就来看下这种新类型的checkpoint的设计 generic log-based incremental checkpo…

js 七种继承方法

目录 1. 第一种方法:原型链继承 2. 第二种方法:构造函数继承(call继承) 3. 第三种方法:组合式继承 4. 第四种方法:拷贝继承 5. 第五种方法:原型式继承 6. 第六种方法

使用pytorch进行图像预处理的常用方法的详细解释

一般来说&#xff0c;我们在使用pytorch进行图像分类任务时都会对训练集数据做必要的格式转换和增广处理&#xff0c;对测试集做格式处理。 以下是常用的数据集处理函数&#xff1a; data_transform { "train": transforms.Compose([transforms.RandomResizedCro…

uniapp门店收银,点击右边商品,商品会进入左边的购物车,并且,当扫码枪扫描商品条形码,商品也会累计进入购物车

效果&#xff1a; 代码&#xff1a; <template><view class"container"><view class"top" style"height: 10%; margin-bottom: 20rpx; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);"><view class"box" style&q…

【HarmonyOS开发】案例-记账本开发

OpenHarmony最近一段时间&#xff0c;简直火的一塌糊度&#xff0c;学习OpenHarmony相关的技术栈也有一段时间了&#xff0c;做个记账本小应用&#xff0c;将所学知识点融合记录一下。 1、记账本涉及知识点 基础组件&#xff08;Button、Select、Text、Span、Divider、Image&am…

【ESP-NOW 入门(ESP32 with Arduino IDE)】

ESP-NOW 入门(ESP32 with Arduino IDE) 1. 前言2. Arduino集成开发环境3. ESP-NOW 简介3.1 ESP-NOW 支持以下功能:3.2 ESP-NOW 技术还存在以下局限性:4. ESP-NOW 单向通信4.1 一个 ESP32 开发板向另一个 ESP32 开发板发送数据4.2 一个“主”ESP32 向多个 ESP32“slave”发送…

Maven配置教程

一&#xff1a;下载 Maven – Download Apache Maven 二&#xff1a;解压 三&#xff1a;修改setting 1.在<localRepository>标签内添加自己的本地位置路径 <!-- localRepository| The path to the local repository maven will use to store artifacts.|| Default:…

IDEA使用HDFS的JavaApi

注&#xff1a;以下代码操作是利用junit在java测试文件夹中实现。 1. 准备工作 1.1 创建测试类 创建测试类&#xff0c;并定义基本变量 public class HDFSJAVAAPI {// 定义后续会用到的基本变量public final String HDFS_PATH "hdfs://hadoop00/";Configuration …

Android studio CMakeLists.txt 打印的内容位置

最近在学习 cmake 就是在安卓中 , 麻烦的要死 , 看了很多的教程 , 发现没有 多少说对打印位置在哪里 , 先说一下版本信息 , 可能你们也不一样 gradle 配置 apply plugin: com.android.applicationandroid {compileSdkVersion 29buildToolsVersion "29.0.3"defau…

electron-builder 打包exe后白屏

项目用的是An Electron application with Vue3 and TypeScript。 Debug运行项目没问题&#xff0c;可以显示页面。不过有浏览器控制台显示错误&#xff1a; Unable to load preload script&#xff1a;preload/index.js Unable to load preload script 翻译后&#xff1a;无法…

GPT编程(1)八分类图像数据集转换为二分类

一个核心问题就是要将这八类数据图片全部重命名&#xff0c;尝试了一步到位 有一个图像数据集&#xff0c;有八个类别amusement,anger,awe,contentment,disgust, excitement, fear,sadness的图片&#xff0c;每张图片被命名为“类别数字”。采用遍历的方式&#xff0c;按顺序阅…

Angular和React有哪些区别?

Angular和React都是流行的前端JavaScript框架&#xff0c;但它们有一些关键的区别&#xff1a; 1、语言&#xff1a; Angular&#xff1a; 使用TypeScript&#xff0c;这是一种强类型的超集&#xff0c;提供了更多的工具和功能&#xff0c;如静态类型检查和更好的代码编辑器支…

uniapp的touchstart与click

移动端的执行顺序&#xff1a;touchstart->touchmove->touchend->click 需求&#xff1a;点击消息查看详情&#xff0c;长按消息执行删除操作 点击事件正常触发&#xff0c;触摸事件正常触发&#xff0c;不会互相影响 问题&#xff1a;再执行删除操作的时候会连带点…

Go语言学习第二天

Go语言数组详解 var 数组变量名 [元素数量]Type 数组变量名&#xff1a;数组声明及使用时的变量名。 元素数量&#xff1a;数组的元素数量&#xff0c;可以是一个表达式&#xff0c;但最终通过编译期计算的结果必须是整型数值&#xff0c;元素数量不能含有到运行时才能确认大小…

阿里云2核2G3M服务器放几个网站?

阿里云2核2g3m服务器可以放几个网站&#xff1f;12个网站&#xff0c;阿里云服务器网的2核2G服务器上安装了12个网站&#xff0c;甚至还可以更多&#xff0c;具体放几个网站取决于网站的访客数量&#xff0c;像阿里云服务器网aliyunfuwuqi.com小编的网站日访问量都很少&#xf…

java 企业工程管理系统软件源码+Spring Cloud + Spring Boot +二次开发+ 可定制化

工程项目管理软件是现代项目管理中不可或缺的工具&#xff0c;它能够帮助项目团队更高效地组织和协调工作。本文将介绍一款功能强大的工程项目管理软件&#xff0c;该软件采用先进的Vue、Uniapp、Layui等技术框架&#xff0c;涵盖了项目策划决策、规划设计、施工建设到竣工交付…

springboot整合hadoop遇错

错误一&#xff1a; Caused by: java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unset. 解决&#xff1a; 下载&#xff1a;https://github.com/steveloughran/winutils 选择一个版本 例如&#xff1a;3.0.0 &#xff0c;将里面的hadoop.dll文件复制…

在IntelliJ IDEA中精通Git配置与使用:全面指南

目录 1 前言2 idea中使用git的准备2.1 在 IntelliJ IDEA 中配置 Git2.2 配置 Git 忽略文件 3 在IntelliJ IDEA中使用Git的基本步骤3.1 项目导入到 Git3.2 查看与切换版本信息 4 在 IntelliJ IDEA 中使用分支4.1 创建分支4.2 无冲突合并4.3 冲突合并 5 结语 1 前言 版本控制是现…