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 this round.
  2. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
  3. "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.
  4. "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.
 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的更多相关文章

  1. Simulation of empirical Bayesian methods (using baseball statistics)

    Previously in this series: The beta distribution Empirical Bayes estimation Credible intervals The B ...

  2. [LeetCode] Baseball Game 棒球游戏

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

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

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

  5. LeetCode 682 Baseball Game 解题报告

    题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...

  6. LeetCode - Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

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

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

  9. Programming Assignment 3: Baseball Elimination

    编程作业三 作业链接:Baseball Elimination & Checklist 我的代码:BaseballElimination.java 问题简介 这是一个最大流模型的实际应用问题: ...

  10. 682. Baseball Game 棒球游戏 按字母处理

    [抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...

随机推荐

  1. 洛谷P4501/loj#2529 [ZJOI2018]胖(ST表+二分)

    题面 传送门(loj) 传送门(洛谷) 题解 我们对于每一个与宫殿相连的点,分别计算它会作为多少个点的最短路的起点 若该点为\(u\),对于某个点\(p\)来说,如果\(d=|p-u|\),且在\([ ...

  2. 文件操作 - 整体操作&文件搜索

    文件操作 - 整体操作 1.touch 作用:创建普通文件 格式:touch file1 [file2] 2.cp 作用:拷贝文件 格式:cp 源文件 目标文件 3.rm 作用:删除文件 格式:rm ...

  3. Windows NLB搭配IIS的ARR搭建高可用环境(转载)

    原文地址:http://www.cnblogs.com/shanyou/archive/2010/04/28/1723276.html 在现行的许多网络应用中,有时一台服务器往往不能满足客户端的要求, ...

  4. js去重方法

    function remove(array){ var obj={}; newarray=[]; for(var i in array){ console.log(i); var arg=array[ ...

  5. Windows 安装Angular CLI

    1.安装nvm npm cnpm nrm(onenote笔记上有记录) 参考:https://blog.csdn.net/tyro_java/article/details/51232458 提示:如 ...

  6. React笔记:ref注意事项

    [一]使用ref必须用在[类型式的组件]才起作用,用在[函数式的组件]是无效的. 下面这个例子用在了[函数式的组件]上,所以是无效的: function MyFunctionalComponent() ...

  7. 洛谷 P4568 [JLOI2011]飞行路线

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

  8. BZOJ 2935/ Poi 1999 原始生物

    [bzoj2935][Poi1999]原始生物   2935: [Poi1999]原始生物 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 145  So ...

  9. BZOJ - 2115 独立回路 线性基

    题意:给定一个图集\((V,E)\),求路径\(1...n\)的最大异或和,其中重复经过的部分也会重复异或 所求既任意一条\(1...n\)的路径的异或和,再异或上任意独立回路的组合的异或和(仔细想想 ...

  10. java语言的各种输入情况-ACM

    1.只输入一组数据: Scanner s=new Scanner(System.in);int a=s.nextInt();int b=s.nextInt(); 2.输入有多组数据,没有说明输入几组数 ...