[leetcode] 682. Baseball Game @ python

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 following types: Integer(one round's score): Directly represents the number of points you get in this round. "+"(one round's score): Represents that th.. 阅读详情

原题

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 two valid round’s points.
“D” (one round’s score): Represents that the points you get in this round are the doubled data of the last valid round’s points.
“C” (an operation, which isn’t a round’s score): Represents the last valid round’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.

解法

构造列表p储存有效的数字, 遍历ops, 当op为数字时, 将op转化为整数放入p, 其余的按照题意来处理p, 这里注意当分数为负时, op.isdigit()为False, 此时我们要将分数为负的情况加进来.
Time: O(n)
Space: O(n)

代码

class Solution(object):
    def calPoints(self, ops):
        """
        :type ops: List[str]
        :rtype: int
        """
        p = []
        for op in ops:
            if op.isdigit() or (op[0] == '-' and len(op) > 1):
                p.append(int(op))
            elif op == '+':
                p.append(p[-1] + p[-2])
            elif op == 'D':
                p.append(2*p[-1])
            elif op == 'C':
                p.pop()
        return sum(p)
GD32主程序跑飞?可能是BOOT0引脚惹的祸(附J-LINK调试实录) 本文针对GD32微控制器程序下载后无法运行的典型问题,深入剖析了BOOT0引脚配置对启动模式的关键影响。通过结合J-LINK调试器与KEIL MDK环境,详细演示了如何从程序跑飞现象(如PC指向0x1FFF Fxxx)定位到硬件层面的BOOT0引脚与VDD短路故障,并提供了系统的硬件排查步骤与设计预防建议。 阅读详情

相关推荐

COMSOL仿真多模光纤弯曲损耗:从原理到工程实践

光纤弯曲损耗是光通信与光纤传感领域的基础物理现象,其本质是光波导结构变形导致模式耦合与能量泄漏。从波动光学原理出发,当光纤弯曲时,其有效折射率分布发生改变,破坏了全反射条件,使得高阶导模转化为辐射模,从而产生损耗。这一过程对光纤通信系统的传输距离、稳定性以及微型化光纤设备的设计至关重要。利用COMSOL Multiphysics的多物理场仿真能力,特别是其波动光学模块与变换光学方法,可以高效、精确地模拟弯曲半径、折射率剖面等参数对损耗的影响。该方法为光通信设计工程师和科研人员提供了强大的虚拟原型验证工具,能

weixin_31188927的博客 299

LWC 51:682. Baseball Game

LWC 51:682. Baseball Game传送门:682. Baseball GameProblem: 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

Demon-初来驾到 631

深入理解搜索引擎——搜索评价指标

    搜索引擎,在做好query理解、索引召回以及排序模型之后,就能直接推上线了吗?答案是否定的,还需对其性能和质量进行评测。性能无非是对时间和空间的运行效率作评测,不细讲,今天讲讲搜索引擎的质量评测。通过质量评测,可根据评测结果不断的进行改进和研究,也可以验证搜索引擎在真实环境中运行时的实际效果。因此,搜索引擎的质量评测对于整个系统的研制和发展是至关重要的。     那么有哪些指标可以评估搜索引擎的结果质量呢?最广为人知的

我是药老的博客 4490

lintcode 983. 棒球游戏

现在你是棒球比赛分记录员。 给定一个字符串数组,每一个字符串可以是以下4种中的其中一个: 整数 (一个回合的分数): 直接表示这回合你得到的分数。 “+” (一个回合的分数): 表示这回合你获得的分数为前两个 有效 分数之和。 “D” (一个回合的分数): 表示这回合你得到的分数为你上一次获得的有效分数的两倍。 “C” (一种操作,而非一个回合的分数): 表示你上回合的有效分数是无效的,需要移除。...

sinb妃的博客 271

Ubuntu Server 22.04.5系统安装教程

该教程主要是演示在服务器上安装Ubuntu server 22.04系统的详细步骤,主要包含,安装系统前的准备(镜像的下载,启动盘的制作),安装系统时的语言选择,网络设置,切换镜像源(可根据实际需求来配置,本文使用清华镜像源),手动分区,用户设置,ssh服务安装等。以上就是关于Ubuntu Server 22.04系统安装的详细过程。

逆水行舟,不进则退。 7832

科研党福音:手把手教你用HYSPLIT做后向轨迹分析(附GDAS1气象数据下载避坑指南)

本文为科研人员提供了一份详细的HYSPLIT后向轨迹分析指南,包括环境准备、GDAS1气象数据下载避坑、核心操作流程及故障排除技巧。通过实战案例和自动化脚本,帮助用户高效完成污染物来源分析,提升科研效率。

weixin_30500105的博客 553

【LeetCode】682. Baseball Game 解题报告(Python)

【LeetCode】682. Baseball Game 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/baseball-game/description/ 题目描述: You’re now a baseball game point recorder. Given a list of strings, ea

负雪明烛 1499

Dual-stream multiple instance learning networks for tumor detection in Whole Slide Image——论文笔记

关于多示例学习的资料网上比较少,但是在医学图像方面还是有人在应用的。主要是因为一般比较大,几个G都是十分常见的,这么大图片是无法直接训练,一般都会进行切图,所以天然契合多示例学习bag和instance的概念。最近看了一篇论文刚好关于这方面的,所以做下笔记。 github地址:https://github.com/binli123/dsmil-wsi paper:https://arxiv.org/abs/2011.08939 背景知识 在多示例学习(MIL)中,一组训练样本被视为包含多个instance的

weixin_41693877的博客 4179

python--leetcode682. Baseball Game

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 g

wenqiwenqi123的博客 1915

拆解爆火AI图文日报!用扣子(Coze)工作流,10分钟实现自动化新闻早报

当用户提供一则新闻内容时,仔细分析新闻中的关键信息。提取新闻事件的核心要点,包括事件主体、发生时间、地点、主要情节等。用简洁的语言对新闻进行总结,控制在[X]字以内(可根据实际需求设定字数限制)。若用户提供多条新闻,对比各新闻之间的异同点。从事件类型、影响范围、发展趋势等方面进行分析。综合总结多条新闻的整体情况,以有条理的方式呈现给用户。

一身都是月 2436

SAP 系统间信任关系配置全攻略:从 SM59 到 SMT1 的实践与风险控制

本文介绍了在SAP系统间配置信任关系的详细流程与最佳实践。文章以SAP Gateway与后端系统架构为场景,从SM59创建RFC目的地开始,到SMT1建立信任关系,再到授权对象S_RFCACL的配置,提供了完整的操作指南。重点包括:使用标准命名规范创建专用RFC连接、通过向导完成系统间信任登记、严格控制S_RFCACL授权范围等。文中还分享了真实项目案例、可复用的检查清单以及常见问题的排查思路,帮助用户实现稳定可靠的跨系统无密码调用,同时确保安全性。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 331

【两次过】Lintcode 983: Baseball Game

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...

majichen95的博客 210

手把手教你学Simulink--电机电磁兼容与可靠性​场景示例:基于Simulink的电机轴电压与轴电流抑制仿真

本文带你完成了电机轴电压与轴电流抑制的Simulink仿真,实现了:✅ 揭示了共模电压 → 寄生电容 → 轴电压 → 轴承电蚀的完整链路✅ 搭建了含寄生参数的高频电机等效模型共模电压最小化PWM(源头抑制)dv/dt滤波器(路径阻断)轴接地(终端泄放) ✅ 通过仿真验证了轴电压从68 V降至<3 V的显著效果 ✅ 为高可靠性电机系统提供低成本、高效益的轴承保护方案。

MHD0815的博客 328

【LeetCode】682. Baseball Game

Python版本:python3.6.2 682. Baseball Game 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 scor

sunflower_kris的博客 317

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode [栈] 682.Baseball Game(C++和Python实现)

682 Baseball Game[难度:简单][stringstream:字符串转整数神器] 【题目】 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 sco...

咸鱼夏の翻身观察记录 310

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

python 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 following types:Integer (one round's score): Directly represents the number of points you get in this...

DreamerLHQ的博客 274

python stack 682. Baseball Game

leetcode 682 计算排球比赛的分数 思想:利用栈的思想,将每个有效得分压入栈中,栈中的分数总和就是总分 class Solution(object): def calPoints(self, ops): """ :type ops: List[str] :rtype: int """ stack=[]...

ZHANGWENJUAN1995的博客 251

【leetcode】682. Baseball Game

33.89%  class Solution(object): def calPoints(self, ops): """ :type ops: List[str] :rtype: int """ result = 0 for i,char in enumerate(ops): ...

u014381464的博客 156

lc682. Baseball Game

682. Baseball Game 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 p...

weixin_34122548的博客 132

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 following types: Integer(one round's score): Directly represents the number of points you...

weixin_30396699的博客 98

[LeetCode&Python] Problem 682. Baseball Game

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 i...

weixin_30279315的博客 110

[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 following types: Integer(one round's score): Directly represents the number of points you get i...

weixin_30496431的博客 128

每日一道Leetcode - 682. 棒球比赛

class Solution(object): def calPoints(self, ops): """ :type ops: List[str] :rtype: int """ result = [] for i in range(len(ops)): if ops[i]=='+': data = result[-1]+result[-2] .

weixin_41041275的博客 140

682. Baseball Game(python+cpp)

You’re now a baseball game point recorder. Given a list of strings, each string can be one of the 4 following types: 1.Integer (one round’s score): Directly represents the number of points you get in...

小湉湉的博客 287

【Leetcode_总结】682. 棒球比赛- python

Q: 你现在是棒球比赛记录员。 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数。 2. "+"(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。 3. "D"(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。 4. "C"(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应...

maka_uir的博客 639

LeetCode 682.棒球比赛(python)

题目链接 题目描述: 你现在是棒球比赛记录员。 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数。 2. “+”(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。 3. “D”(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。 4. “C”(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分...

weixin_44740082的博客 406

Python Leetcode(682.棒球比赛)

Python Leetcode(682.棒球比赛) 你现在是棒球比赛记录员。 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数。 2. “+”(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。 3. “D”(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。 4. “C”(一个操作,这不是一个回合的分数):表示...

虎口脱险的博客 402
上一篇: [leetcode] 339. Nested List Weight Sum @ python
下一篇: [leetcode] 226. Invert Binary Tree @ python
闲庭信步的空间
博客等级 码龄9年 86粉丝 416原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值