[LeetCode] 682. 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 twovalidround's points."D"(one round's score): Represents that the points you get in this round are the doubled data of the lastvalidround's points."C"(an operation, which isn't a round's score): Represents the lastvalidround'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.
解法:栈,用一个stack记录,循环字符串,如果是数字直接进栈,如果当前字符是操作符对栈里面的数字进行相应处理,'+'把栈里面最后两个数字相加然后如栈,'D'把栈里的最后一个数字乘以2然后如栈,'C'移除栈里面的最后一个元素。最后返回栈里所有元素的和。
Java:
class Solution {
public int calPoints(String[] ops) {
int sum = 0;
LinkedList<Integer> list = new LinkedList<>();
for (String op : ops) {
if (op.equals("C")) {
sum -= list.removeLast();
}
else if (op.equals("D")) {
list.add(list.peekLast() * 2);
sum += list.peekLast();
}
else if (op.equals("+")) {
list.add(list.peekLast() + list.get(list.size() - 2));
sum += list.peekLast();
}
else {
list.add(Integer.parseInt(op));
sum += list.peekLast();
}
}
return sum;
}
}
Python:
class Solution(object):
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
history = []
for op in ops:
if op == '+':
history.append(history[-1] + history[-2])
elif op == 'D':
history.append(history[-1] * 2)
elif op == 'C':
history.pop()
else:
history.append(int(op))
return sum(history)
C++:
class Solution {
public:
int calPoints(vector<string>& ops) {
vector<int> v;
for (string op : ops) {
if (op == "+") {
v.push_back(v.back() + v[v.size() - 2]);
} else if (op == "D") {
v.push_back(2 * v.back());
} else if (op == "C") {
v.pop_back();
} else {
v.push_back(stoi(op));
}
}
return accumulate(v.begin(), v.end(), 0);
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 682. Baseball Game 棒球游戏的更多相关文章
- 682. Baseball Game 棒球游戏 按字母处理
[抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...
- [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 682 Baseball Game 解题报告
题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...
- 【LeetCode】682. Baseball Game 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈模拟 日期 题目地址:https://leet ...
- 【Leetcode_easy】682. Baseball Game
problem 682. Baseball Game solution: 没想到使用vector! class Solution { public: int calPoints(vector<s ...
- Java实现 LeetCode 682 棒球比赛(暴力)
682. 棒球比赛 你现在是棒球比赛记录员. 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数. 2. "+"(一轮的 ...
- [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 ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
随机推荐
- Gorgeous Sequence(HDU5360+线段树)
题目链接 传送门 题面 思路 对于线段树的每个结点我们存这个区间的最大值\(mx\).最大值个数\(cnt\).严格第二大数\(se\),操作\(0\): 如果\(mx\leq val\)则不需要更新 ...
- Python应用之-修改通讯录
#-*- coding:utf-8 -*- import sqlite3 #打开本地数据库用于存储用户信息 conn = sqlite3.connect('mysql_person.db') #在该数 ...
- Hive UDF函数构建
1. 概述 UDF函数其实就是一个简单的函数,执行过程就是在Hive转换成MapReduce程序后,执行java方法,类似于像MapReduce执行过程中加入一个插件,方便扩展.UDF只能实现一进一出 ...
- 《团队作业第三、第四周》五阿哥团队作业--Scrum 冲刺阶段--Day1--领航
<团队作业第三.第四周>五阿哥团队作业--Scrum 冲刺阶段--Day1--领航 各个成员在 Alpha 阶段认领的任务 在团队合作时任务也会动态分配,最终以实际为主,上述具有参考价值. ...
- Linux Shell 常用命令与目录分区的学习总结
很早就想根据自己的学习规律和遗忘规律,自己总结一下Linux/Unix系统的Shell命令,一来便于自己时常查询之用,二来也分享于各位博友 Linux shell是系统的用户界面,即命令行.它提供了用 ...
- 自定义express中间件
const http = require('http') class LikeExpress { constructor() { this.middleList = [] this.routes = ...
- Java 内部类和Lambda
Java内部类 内部类又称为嵌套类,是在类中定义另外一个类.内部类可以处于方法内/外,内部类的成员变量/方法名可以和外部类的相同.内部类编译后会成为完全不同的两个类,分别为outer.class和ou ...
- 六.深浅copy
先问问大家,什么是拷贝?拷贝是音译的词,其实他是从copy这个英文单词音译过来的,那什么是copy? copy其实就是复制一份,也就是所谓的抄一份.深浅copy其实就是完全复制一份,和部分复制一份的意 ...
- Comet OJ - Contest #14 转转的数据结构题 珂朵莉树+树状数组
题目链接: 题意:有两个操作 操作1:给出n个操作,将区间为l到r的数字改为x 操作2:给出q个操作,输出进行了操作1中的第x到x+y-1操作后的结果 解法: 把询问离线,按照r从小到大排序 每次询问 ...
- 洛谷 P4281 [AHOI2008] 紧急集合 题解
挺好的一道题,本身不难,就把求两个点的LCA变为求三个点两两求LCA,不重合的点才是最优解.值得一提的是,最后对答案的处理运用差分的思想:假设两点 一点深度为d1,另一点 深度为d2,它们LCA深度为 ...