当前位置:网站首页>【LeetCode每日一题】——682.棒球比赛
【LeetCode每日一题】——682.棒球比赛
2022-08-11 06:28:00 【IronmanJay】
一【题目类别】
- 栈
二【题目难度】
- 简单
三【题目编号】
- 682.棒球比赛
四【题目描述】
- 你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。
- 比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作,ops 遵循下述规则:
- 整数 x - 表示本回合新获得分数 x
- “+” - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。
- “D” - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。
- “C” - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。
- 请你返回记录中所有得分的总和。
五【题目示例】
示例 1:
- 输入:ops = [“5”,“2”,“C”,“D”,“+”]
- 输出:30
- 解释:
“5” - 记录加 5 ,记录现在是 [5]
“2” - 记录加 2 ,记录现在是 [5, 2]
“C” - 使前一次得分的记录无效并将其移除,记录现在是 [5].
“D” - 记录加 2 * 5 = 10 ,记录现在是 [5, 10].
“+” - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15].
所有得分的总和 5 + 10 + 15 = 30
示例 2:
- 输入:ops = [“5”,“-2”,“4”,“C”,“D”,“9”,“+”,“+”]
- 输出:27
- 解释:
“5” - 记录加 5 ,记录现在是 [5]
“-2” - 记录加 -2 ,记录现在是 [5, -2]
“4” - 记录加 4 ,记录现在是 [5, -2, 4]
“C” - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2]
“D” - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4]
“9” - 记录加 9 ,记录现在是 [5, -2, -4, 9]
“+” - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5]
“+” - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14]
所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27
示例 3:
- 输入:ops = [“1”]
- 输出:1
六【题目提示】
- 1 <= ops.length <= 1000
- ops[i] 为 “C”、“D”、“+”,或者一个表示整数的字符串。整数范围是 [ − 3 ∗ 1 0 4 , 3 ∗ 1 0 4 ] [-3 * 10^4, 3 * 10^4] [−3∗104,3∗104]
- 对于 “+” 操作,题目数据保证记录此操作时前面总是存在两个有效的分数
- 对于 “C” 和 “D” 操作,题目数据保证记录此操作时前面总是存在一个有效的分数
七【解题思路】
- 利用栈的思想
- 遇到数字存入栈
- 遇到+将栈顶元素和次栈顶元素相加再入栈,注意这里并不弹出栈,因为还要记录之前回合的分数
- 遇到D将栈顶元素乘2后再入栈
- 遇到C将栈顶元素弹出,说明前一回合的分数作废
- 最后将所有回合的分数累加求和即可
八【时间频度】
- 时间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组元素个数
- 空间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组元素个数
九【代码实现】
- Java语言版
package Stack;
public class p682_BaseballGame {
public static void main(String[] args) {
String[] ops = {
"5", "2", "C", "D", "+"};
int res = calPoints(ops);
System.out.println("res = " + res);
}
public static int calPoints(String[] ops) {
int[] stack = new int[ops.length];
int top = 0;
int res = 0;
for (String op : ops) {
switch (op.charAt(0)) {
case '+':
int temp1 = stack[top - 1];
int temp2 = stack[top - 2];
stack[top++] = temp1 + temp2;
break;
case 'D':
int temp = stack[top - 1] * 2;
stack[top++] = temp;
break;
case 'C':
top--;
break;
default:
stack[top++] = Integer.parseInt(op);
break;
}
}
for (int i = 0; i < top; i++) {
res += stack[i];
}
return res;
}
}
- C语言版
#include<stdio.h>
int calPoints(char ** ops, int opsSize)
{
int* stack = (int*)malloc(sizeof(int) * opsSize);
int top = 0;
int res = 0;
for (int i = 0; i < opsSize; i++)
{
switch (ops[i][0])
{
case '+':
stack[top++] = stack[top - 1] + stack[top - 2];
break;
case 'D':
stack[top++] = stack[top - 1] * 2;
break;
case 'C':
top--;
break;
default:
stack[top++] = atoi(ops[i]);
break;
}
}
for (int i = 0; i < top; i++)
{
res += stack[i];
}
return res;
}
/*主函数省略*/
十【提交结果】
Java语言版
C语言版
边栏推荐
- maxwell concept
- Amazon API interface Daquan
- Pinduoduo api interface application example
- Taobao API interface reference
- Redis源码-String:Redis String命令、Redis String存储原理、Redis字符串三种编码类型、Redis String SDS源码解析、Redis String应用场景
- Attitude solution - gyroscope + Euler method
- 2022-08-10 Group 4 Self-cultivation class study notes (every day)
- Discourse 的关闭主题(Close Topic )和重新开放主题
- 每日sql -用户两天留存率
- Coordinate system in navigation and positioning
猜你喜欢
mmdetection的安装和训练、测试didi数据集的步骤(含结果)
强烈推荐一款好用的API接口
公牛10-11德里克·罗斯最强赛季记录
EasyPlayer针对H.265视频不自动播放设置下,loading状态无法消失的解决办法
【软件测试】(北京)字节跳动科技有限公司二面笔试题
如何选择专业、安全、高性能的远程控制软件
Daily sql: request for friend application pass rate
Daily sql-statistics of the number of professionals (including the number of professionals is 0)
获取拼多多商品信息操作详情
姿态解算-陀螺仪+欧拉法
随机推荐
Implement general-purpose, high-performance sorting and quicksort optimizations
2022-08-10 Group 4 Self-cultivation class study notes (every day)
《猪猪1984》NFT 作品集将上线 The Sandbox 市场平台
pytorch,numpy两种方法实现nms类间+类内
抖音分享口令url API工具
MySQL使用GROUP BY 分组查询时,SELECT 查询字段包含非分组字段
公牛10-11德里克·罗斯最强赛季记录
concept noun
Daily sql-employee bonus filtering and answer rate ranking first
软件测试基本流程有哪些?北京专业第三方软件检测机构安利
亚马逊获得AMAZON商品详情 API 返回值说明
抖音关键词搜索商品-API工具
Edge 提供了标签分组功能
NTT的Another Me技术助力创造歌舞伎演员中村狮童的数字孪生体,将在 “Cho Kabuki 2022 Powered by NTT”舞台剧中首次亮相
一种用于EEG超扫描研究的分析流程
ssh服务攻防与加固
Resolved EROR 1064 (42000): You have an error in. your SOL syntax. check the manual that corresponds to yo
联想集团:2022/23财年第一季度业绩
ROS 服务通信理论模型
radix-4 FFT principle and C language code implementation