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 ...
随机推荐
- 【转】WinForm时间选择控件(DateTimePicker)如何选择(显示)时分秒
源地址:https://www.cnblogs.com/EvanFan/p/7826160.html 注意:年月日时分秒的大小写格式,如果错了数据就错了,如果能显示当前时间说明设置正确
- 51nod1832(二叉树/高精度模板+dfs)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1832 题意: 中文题诶~ 思路: 若二叉树中有 k 个节点只 ...
- luoguP2387 [NOI2014]魔法森林
https://www.luogu.org/problemnew/show/P2387 考虑先将所有边按 a 值排序,依次加入每一条边,如果这条边的两个端点 ( l, r ) 之间的简单路径中 b 的 ...
- apache2.4配置
首先修改httpd.conf配置文件. vim conf/httpd.conf 添加: Listen 1234 然后把 # Virtual hosts #Include conf/extra/ ...
- centos7用docker安装kafka
参考之前的文章安装zookeeper集群 https://www.cnblogs.com/xiaohanlin/p/10124674.html 如果是测试环境也可以简单安装单节点的zookeeper ...
- Python——selenium爬取斗鱼房间信息
from selenium import webdriver import os import json import time class Douyu: def __init__(self): # ...
- Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample: 错误
在Maven工程下,想通过controller接收url中的参数Id查询数据库的数据,并且以json形式显示在页面. 在浏览器输入url后出现的问题: 八月 22, 2016 1:45:22 下午 o ...
- LOSKI,我
2019年入驻github以及博客园 在发现用github的issue写博客稍微有些奇怪后决定开辟这个更适合写博的空间 2019/4/1 目前大一,计算机专业,尚未分流 更多的时间花在了数据结构与算法 ...
- php 百万级数据文件导出
背景:最近一个需求是从mysql里面读取数据(有点大),然后导出为csv文件 问题 :发现导出的过程中自己中断了,没有报错 . 第一反应是测试导出一个小一点的数据(少查几条),发现没问题 . 查看导出 ...
- win10+anaconda环境下pyqt5+qt tools+eric6.18安装及汉化过程
最近需要用python编写一个小程序的界面,选择了pyqt5+eric6的配套组合,安装过程中遇到一些坑,特此记录.参考书籍是电子工业出版社的<PyQt5快速开发与实战>. 因为我使用an ...