[抄题]:

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. 把color转成image的方法

    - (UIImage*)createImageWithColor:(UIColor*) color { CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); ...

  2. LA3263 That Nice Euler Circuits

    题意 PDF 分析 欧拉定理:设平面内顶点数.边数.面数分别为\(V,E,F\),则\(V+F-E=2\). 枚举每对线段求交点,注意去重. 另外注意第n个端点和第一个端点重合. 时间复杂度\(o(T ...

  3. 关于setdefault和defaultdict

    c参考链接:http://blog.csdn.net/real_ray/article/details/17919289 defaultdict就是为没有的键给一个默认的值,实际是实现了一个__mis ...

  4. 【Swift 】- 闭包

    闭包是自包含带函数代码块,可以在代码中被传递和使用.我觉得可以这样理解:闭包相当于C#中的lambda表达式: 全局函数和嵌套函数,实际也是特殊的闭包. 通常闭包是以下三种形式: a,全局函数是一个有 ...

  5. 简述FPGA的一些优势

    优势一: 更大的并行度.这个主要是通过并发和流水两种技术实现. A:并发是指重复分配计算资源,使得多个模块之间可以同时独立进行计算.这一点与现在的多核和SIMD技术相似.但相对与SIMD技术,FPGA ...

  6. mac 在终端使用命令行启动脚本,无法使用自己安装的python去执行脚本问题

    参考了2片文章: //查看python位置 which python //先备份 1.sudo cp /usr/bin/python /usr/bin/python_cp //删除 2.sudo rm ...

  7. unittest之跳过用例(skip) (含如何调用类里面函数相互调取变量的方法)

    当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例也就没 ...

  8. JVM之运行时数据区

    Java虚拟机运行时数据区包括PC寄存器.Java虚拟机栈.Java堆.方法区.本地方法栈.运行时常量池六个部分. 1. PC寄存器 PC寄存器(又叫程序计数器,Program Counter Reg ...

  9. ES之一:Elasticsearch6.4 windows安装 head插件ik分词插件安装

    准备安装目标:1.Elasticsearch6.42.head插件3.ik分词插件 第一步:安装Elasticsearch6.4 下载方式:1.官网下载 https://www.elastic.co/ ...

  10. mina中责任链模式的实现

    一.mina的框架回顾 责任链模式在mina中有重要的作用,其中Filter机制就是基于责任链实现的. 从上图看到消息的接受从IoService层先经过Filter层过滤处理后最后交给IoHander ...