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 the points you get in this round are the sum of the last twovalid
round's points."D"
(one round's score): Represents that the points you get in this round are the doubled data of the lastvalid
round's points."C"
(an operation, which isn't a round's score): Represents the lastvalid
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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- string数组中的元素是string,不是char。用双引号。但是字符串也可以用一个c字母来表示。
- 两倍的情况不能忘了push回原来的值
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
stack起作用的时候,不外乎就是push pop方法
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
Integer.parseInt 用整数类,把字符转化成整数了
[关键模板化代码]:
int temp1 = stack.pop();
int temp2 = stack.pop();
int tempSum = temp1 + temp2;
sum += tempSum;
stack.push(temp2);
stack.push(temp1);
stack.push(tempSum);
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int calPoints(String[] ops) {
//cc
if (ops.length == 0) {
return 0;
} //ini: stack, sum
Stack<Integer> stack = new Stack<>();
int sum = 0; //4 states
for (String c : ops) {
if (c.equals("+")) {
int temp1 = stack.pop();
int temp2 = stack.pop();
int tempSum = temp1 + temp2;
sum += tempSum;
stack.push(temp2);
stack.push(temp1);
stack.push(tempSum);
}
else if (c.equals("D")) {
int temp = stack.pop();
int temp_d = temp * 2;
sum += temp_d;
stack.push(temp);
stack.push(temp_d);
}
else if (c.equals("C")) {
int del = stack.pop();
sum -= del;
}
else {
int temp = Integer.parseInt(c);
sum += temp;
stack.push(temp);
}
} return sum;
}
}
682. Baseball Game 棒球游戏 按字母处理的更多相关文章
- [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 ...
- [LeetCode] Baseball Game 棒球游戏
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- 【LeetCode】682. Baseball Game 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈模拟 日期 题目地址:https://leet ...
- 【Leetcode_easy】682. Baseball Game
problem 682. Baseball Game solution: 没想到使用vector! class Solution { public: int calPoints(vector<s ...
- LeetCode 682 Baseball Game 解题报告
题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...
- [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 ...
- Leetcode682.Baseball Game棒球比赛
你现在是棒球比赛记录员. 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数. 2. "+"(一轮的得分):表示本轮获得 ...
- 682. Baseball Game
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 682. Baseball Game (5月28日)
解答(打败98.60%) class Solution { public: int calPoints(vector<string>& ops) { vector<int&g ...
随机推荐
- linux 系统统计目录下文件夹的大小
du -ah --max-depth=1 这个是我想要的结果 a表示显示目录下所有的文件和文件夹(不含子目录),h表示以人类能看懂的方式,max-depth表示目录的深度. du命令用来查看 ...
- 21天学通C++_Day3_Part2
0.语句的分行 法1:在第一行末尾添加反斜杠 cout<<"Hello \ World!"<<endl; 法2:将字符串字面量分成两个,编译器注意到两个响铃 ...
- 模糊聚类算法(FCM)
伴随着模糊集理论的形成.发展和深化,RusPini率先提出模糊划分的概念.以此为起点和基础,模糊聚类理论和方法迅速蓬勃发展起来.针对不同的应用,人们提出了很多模糊聚类算法,比较典型的有基于相似性关系和 ...
- Windows 系统定时自动重启
1.创建新文本并输入 shutdown -r -t 0 保存成.bat文件 2.创建系统任务计划 2.1 在开始中打开[任务计划程序] 2.2 新建创建任务计划目录 2.3 在新目录下新建任务计划即可 ...
- [Luogu3538][POI2012]OKR-A Horrible Poem
luogu 题意 给出一个由小写英文字母组成的字符串\(S\),再给出\(q\)个询问,要求回答\(S\)某个子串的最短循环节. 如果字符串\(B\)是字符串\(A\)的循环节,那么\(A\)可以由\ ...
- 一分钟理解js闭包
什么是闭包?先看一段代码: ? 1 2 3 4 5 6 7 8 9 10 function a(){ var n = 0; function inc() { n++; cons ...
- IronPython之基本类型
通过下图展现IronPython的基本类型,便于理解和记忆. 基本数据类型 数据类型 类型 示例 备注 Byte string str ‘hello’ “hello” “””hello””” ‘’’h ...
- IIS安装步骤(WIN10)
打开控制面板 点开程序 点击“启动或关闭Windows功能,进入到启用或关闭windows功能之后我们选中“Internet Infomation Services”并勾选 点击确定 ...
- 洛谷 1600 (NOIp2016) 天天爱跑步——树上差分
题目:https://www.luogu.org/problemnew/show/P1600 看TJ:https://blog.csdn.net/clove_unique/article/detail ...
- FTP上传失败报错227 Entering Passive Model (222,111,8,111,10,40)
昨天为了一个ftp问题折腾了一天.问题背景:原来有个接口涉及到上传文件,服务端更换了ftp服务器,我们这边需要刷新连接服务端的ip和端口配置,代码没动.联调环境和验收环境都测试通过,一到生产环境就歇菜 ...