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. shell-脚本-作用是发现前10个最占用磁盘空间的文件

    #!/bin/bash## 这个脚本的作用是发现前10个最占用磁盘空间的文件################定义这个脚本的全区变量##################################C ...

  2. LAMP课程(3)

    LAMP课程(3) 一.bash的使用 1.1.输出重定向 >:覆盖输出(写入内容) 具体实例1:将内容写入到文件中   >>:追加输出 具体实例2:   1.2 && ...

  3. 当AVPlayer在被释放之后,Player一直监听的时间没有被移除,提示错误的解决办法

    Xcode Consolu打印出来的提示: An instance 0x156608c0 of class AVPlayer was deallocated while key value obser ...

  4. python语法学习面向对象之继承

    python语法学习面向对象之继承 转载自:http://www.cnblogs.com/Joans/archive/2012/11/09/2757368.html 只要涉及到面向对象,”类“是必须出 ...

  5. Jprofiler的安装部署及使用

    本地与远程安装同版本的jprofiler.以本地Windows操作系统,远程AIX操作系统为例,详细介绍安装配置步骤.本次测试使用的均是jp6版本. 一.安装Jprofiler服务端 一 般情况下,J ...

  6. Macaca,Maven,MVC框架

    Macaca:Macaca是阿里开源的一套完整的自动化测试解决方案.同时支持PC和移动端测试,支持的语言有JS,Java,Python. Maven:java,Maven项目对象模型(POM),可以通 ...

  7. Apache https 证书配置...

    没啥好说的..赋值粘贴 !! Listen 443 SSLSessionCache "shmcb:/apache/logs/ssl_scache(512000)" SSLSessi ...

  8. Django Cannot assign "A1": "B1" must be a "C1" instance. 错误信息

    常见报错 Cannot assign “A1”: “B1” must be a “C1” instance. 告诉我们 必须使用 C1 模型类的 实例,而不是具体的参数值. 这个错误信息,是我写入数据 ...

  9. [转]【NODE】用WS模块创建加密的WS服务(WSS)

    [From] https://luojia.me/2015/07/21/%E3%80%90node%E3%80%91%E7%94%A8ws%E6%A8%A1%E5%9D%97%E5%88%9B%E5% ...

  10. (转)数位dp

    原博客 https://blog.csdn.net/wust_zzwh/article/details/52100392 建议原博客看到hdu 不要62,然后看我分割线后两道题,然后再回来看原博.-- ...