[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. string数组中的元素是string,不是char。用双引号。但是字符串也可以用一个c字母来表示。
  2. 两倍的情况不能忘了push回原来的值

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

stack起作用的时候,不外乎就是push pop方法

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

Integer.parseInt 用整数类,把字符转化成整数了

[关键模板化代码]:

          int temp1 = stack.pop();
int temp2 = stack.pop();
int tempSum = temp1 + temp2;
sum += tempSum;
stack.push(temp2);
stack.push(temp1);
stack.push(tempSum);

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int calPoints(String[] ops) {
//cc
if (ops.length == 0) {
return 0;
} //ini: stack, sum
Stack<Integer> stack = new Stack<>();
int sum = 0; //4 states
for (String c : ops) {
if (c.equals("+")) {
int temp1 = stack.pop();
int temp2 = stack.pop();
int tempSum = temp1 + temp2;
sum += tempSum;
stack.push(temp2);
stack.push(temp1);
stack.push(tempSum);
}
else if (c.equals("D")) {
int temp = stack.pop();
int temp_d = temp * 2;
sum += temp_d;
stack.push(temp);
stack.push(temp_d);
}
else if (c.equals("C")) {
int del = stack.pop();
sum -= del;
}
else {
int temp = Integer.parseInt(c);
sum += temp;
stack.push(temp);
}
} return sum;
}
}

682. Baseball Game 棒球游戏 按字母处理的更多相关文章

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

  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. 【LeetCode】682. Baseball Game 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈模拟 日期 题目地址:https://leet ...

  4. 【Leetcode_easy】682. Baseball Game

    problem 682. Baseball Game solution: 没想到使用vector! class Solution { public: int calPoints(vector<s ...

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

  7. Leetcode682.Baseball Game棒球比赛

    你现在是棒球比赛记录员. 给定一个字符串列表,每个字符串可以是以下四种类型之一: 1.整数(一轮的得分):直接表示您在本轮中获得的积分数. 2. "+"(一轮的得分):表示本轮获得 ...

  8. 682. Baseball Game

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 682. Baseball Game (5月28日)

    解答(打败98.60%) class Solution { public: int calPoints(vector<string>& ops) { vector<int&g ...

随机推荐

  1. SHELL 脚本----常用的命令

    一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂   建立一个脚本 Linux中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行she ...

  2. 【学习】JennyHui学英语 - Bingo口语笔记

    学习素材:Bingo说美语 Hold系列 如何吞音 表示“无所谓” Pick系列 英式和美式英语的发音区别 表示“迷茫” Break系列 爆破音发音技巧 表示“不相信” 常见词汇的缩读 表示“身体抱恙 ...

  3. HihoCoder - 1236 Scores (五维偏序,分块+bitset)

    题目链接 题意:给定n个五维空间上的点,以及m组询问,每组询问给出一个点,求五个维度都不大于它的点有多少个,强制在线. 神仙题 单独考虑每个维度,把所有点按这个维度上的大小排序,然后分成T块,每块用一 ...

  4. 《DSP using MATLAB》示例Example7.21

    代码: h = [1, 2, 3, 4, 3, 2, 1]/15; M = length(h); n = 0:M-1; [Hr, w, a, L] = Hr_Type1(h); a L amax = ...

  5. LeetCode Find Duplicate File in System

    原题链接在这里:https://leetcode.com/problems/find-duplicate-file-in-system/description/ 题目: Given a list of ...

  6. 关于ORM,以及Python中SQLAlchemy的sessionmaker,scoped_session

    orm(object relational mapping):对象关系映射. python面向对象,而数据库是关系型. orm是将数据库关系映射为Python中的对象,不用直接写SQL. 缺点是性能略 ...

  7. 阿里云服务器tomcat启动慢解决方案

    yum -y install rng-tools( 熵服务)     cp /usr/lib/systemd/system/rngd.service /etc/systemd/system     v ...

  8. 关于 Android 中未公开的类(用@hide隐藏的类)

    关于 Android 中未公开的类(用@hide隐藏的类) 摘自:http://wangsheng2008love.blog.163.com/blog/static/78201689201142643 ...

  9. C语言回调函数详解

    1. 什么是回调函数? 回调函数,光听名字就比普通函数要高大上一些,那到底什么是回调函数呢?恕我读得书少,没有在那本书上看到关于回调函数的定义.我在百度上搜了一下,发现众说纷纭,有很大一部分都是使用类 ...

  10. 如何查看自己的laravel版本

    方法1: 使用php artisan --version ,只要能看懂这个命令的人一定已经具有初步的Laravel知识.再介绍一种不需要命令,直接去文件中去查看的方法. 方法2: 在项目文件中找ven ...