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.
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.
public int calPoints(String[] ops) {
if(ops == null || ops.length == 0)
return 0;
int []points = new int[ops.length];
int point = -1;
int sum = 0;
for(String op : ops){
if(op.equals("+")){
sum = points[point] + points[point - 1];
points[++ point] = sum;
}else if(op.equals("D")){
sum = points[point] * 2;
points[++ point] = sum;
}else if(op.equals("C")){
point --;
}else{
points[++point] = Integer.parseInt(op);
}
}
sum = 0;
for(int i = 0; i <= point; i++){
sum += points[i];
}
return sum;
}
优化,少遍历一遍
public int calPoints(String[] ops) {
if(ops == null || ops.length == 0)
return 0;
int []points = new int[ops.length];
int point = -1;
int sum = 0;
int temp = 0;
for(String op : ops){
if(op.equals("+")){
temp = points[point] + points[point - 1];
points[++point] = temp;
sum += temp;
}else if(op.equals("D")){
temp = points[point] * 2;
points[++ point] = temp;
sum += temp;
}else if(op.equals("C")){
sum -= points[point];
point --;
}else{
points[++point] = Integer.parseInt(op);
sum += points[point];
}
}
return sum;
}
Baseball Game的更多相关文章
- Simulation of empirical Bayesian methods (using baseball statistics)
Previously in this series: The beta distribution Empirical Bayes estimation Credible intervals The B ...
- [LeetCode] Baseball Game 棒球游戏
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- [Swift]LeetCode682. 棒球比赛 | Baseball Game
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- (string stoi 栈)leetcode682. 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 解题报告
题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...
- 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&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 ...
- Stack-682. Baseball Game
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- Programming Assignment 3: Baseball Elimination
编程作业三 作业链接:Baseball Elimination & Checklist 我的代码:BaseballElimination.java 问题简介 这是一个最大流模型的实际应用问题: ...
- 682. Baseball Game 棒球游戏 按字母处理
[抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...
随机推荐
- 洛谷P3356 火星探险问题(费用流)
传送门 和深海机器人问题差不多……看到有的大佬是用dp过的,强无敌…… 考虑一下,把每一个点拆点,分别是$A_i$和$B_i$,连一条容量为$inf$,费用为$0$的边,表示可以随便走.如果有石头,再 ...
- redis集群如何解决重启不了的问题
redis使用集群部署,如果遇到断电或者服务器重启,当再次启动的时候,有时候会启动不了.需要使用trib的fix命令进行修复.如果修复还是不行的话,可以清除节点数据再重新建集群,前提要备份之后操作. ...
- CentOS7.4关闭防火墙
//临时关闭 systemctl stop firewalld //禁止开机启动 systemctl disable firewalld Removed symlink /etc/systemd/sy ...
- SpeechVoiceSpeakFlags枚举类型的详细解释
http://blog.csdn.net/zhou_xw6511/article/details/8313528
- elasticsearch head 连接不到elasticsearch
配置好head后看到没有正常连接到elasticsearch. 重启后效果:
- bootdo开源项目修改代码后页面无效
修改了JS文件,重启服务后,发现页面没有刷新出效果. 清空缓存一般就可以解决此问题.
- 【算法笔记】B1038 统计同成绩学生
1038 统计同成绩学生 (20 分) 本题要求读入 N 名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第 1 行给出不超过 105 的正整数 N,即学生总人数.随后一行给 ...
- 能量项链 (区间DP)
能量项链 (区间DP) 问题引入 能量项链 洛谷 P1063 思路 诸如此类不能线性规划的问题要用到区间DP,区间DP一般就是三层循环,第一层表示区间长度(本题即\(n\)),第二层枚举起点并根据第一 ...
- SQL批量提交修改业务
把你需要批量提交修改的东西在内存中修改完毕 然后执行以下代码 SqlConnection conn = new SqlConnection(ConnectionString);SqlDataAdapt ...
- 给小程序组件创建slot
<!--comviewonents/juan/juan.wxml--> <view class="model-wrapper" hidden="{{vi ...