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.
今天这个题目有点意思,巨长,而且我做的话感觉逻辑比较复杂。
翻译:
你现在是棒球比赛记录员。
给定一个字符串列表,每个字符串可以是以下四种类型之一:
Integer
(一轮的得分):直接表示你在本轮获得的积分数。"+"
(一轮得分):表示本轮得分是最后两轮valid
得分的总和。"D"
(一轮得分):表示本轮得分是最后valid
一轮得分的两倍。"C"
(一个操作,这不是一个回合的分数):表示valid
你得到的最后一轮的分数是无效的,应该删除。
每一轮的操作都是永久性的,可能会对前一轮和后一轮产生影响。
你需要返回你在所有回合中得分的总和。
事例:
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.
光读题目读明白就用了不少时间。。。。但是我还是面临一个问题,如何去记录前两轮的分数呢?因为他们每进行一次遍历都会改变,而且改变的方式还不同,所以我并不知道这个题该如何下手。
答案:
class Solution {
public int calPoints(String[] ops) {
Stack<Integer> stack = new Stack();//定义一个栈,里面存放Integer类型的得分。
for(String op : ops) {//遍历输入的数组。
if (op.equals("+")) {//如果是+,本轮得分为前两轮得分的和。
int top = stack.pop();//移除栈顶得分,也就是上一轮得分,并记录分数为top。
int newtop = top + stack.peek();//获取现在栈顶分数也就是前两轮的分数(因为前一轮的分数被移除),并且加上前一轮的分数,最后为本轮得分。
stack.push(top);//前一轮的分数作为下一次计算分数时前两轮的分数入栈。
stack.push(newtop);//本轮分数作为下一次计算得分时的前一轮分数入栈。
} else if (op.equals("C")) {//如果是c,则上一轮分数失效。
stack.pop();//直接移除栈顶的分数,也就是上一轮得分。
} else if (op.equals("D")) {//如果是d,则本轮得分为前一轮分数的二倍。
stack.push(2 * stack.peek());直接获取当前栈顶分数也就是上一轮得分的二倍作为本轮分数放入栈。
} else {//其余情况为数字,直接就是本轮得分
stack.push(Integer.valueOf(op));//将遍历出的字符串转为数字直接入栈作为本轮分数。
}
}
int ans = 0;//定义一个变量记录总分
for(int score : stack) ans += score;//遍历栈,里面全是每轮的有效分数,全部相加为最后总分。
return ans;//返回总分数。
}
}
以上是官网中给出的答案,使用了栈来作为容器记录分数,因为对栈的使用相对较少,所以我先去学习来一下java中栈的操作,解析我直接写到了注释中,已经解释的算是很清楚了,嘿嘿。
这道题学会了用栈去解决一些简单的问题,看来大学学的数据结构还是有用的,否则我估计今天就去学习栈就好嘞,不用写题了,哈哈。