1. You're now a baseball game point recorder.
  2.  
  3. Given a list of strings, each string can be one of the 4 following types:
  4.  
  5. Integer (one round's score): Directly represents the number of points you get in this round.
  6. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
  7. "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.
  8. "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.
  9. Each round's operation is permanent and could have an impact on the round before and the round after.
  10.  
  11. You need to return the sum of the points you could get in all the rounds.
  12.  
  13. Example 1:
  14. Input: ["5","2","C","D","+"]
  15. Output: 30
  16. Explanation:
  17. Round 1: You could get 5 points. The sum is: 5.
  18. Round 2: You could get 2 points. The sum is: 7.
  19. Operation 1: The round 2's data was invalid. The sum is: 5.
  20. Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
  21. Round 4: You could get 5 + 10 = 15 points. The sum is: 30.
  22. Example 2:
  23. Input: ["5","-2","4","C","D","9","+","+"]
  24. Output: 27
  25. Explanation:
  26. Round 1: You could get 5 points. The sum is: 5.
  27. Round 2: You could get -2 points. The sum is: 3.
  28. Round 3: You could get 4 points. The sum is: 7.
  29. Operation 1: The round 3's data is invalid. The sum is: 3.
  30. Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
  31. Round 5: You could get 9 points. The sum is: 8.
  32. Round 6: You could get -4 + 9 = 5 points. The sum is 13.
  33. Round 7: You could get 9 + 5 = 14 points. The sum is 27.
  34. Note:
  35. The size of the input list will be between 1 and 1000.
  36. Every integer represented in the list will be between -30000 and 30000.

这道题用stack做,注意stack里面存的元元素是什么。

  1. class Solution {
  2. public int calPoints(String[] ops) {
  3. if(ops == null || ops.length == 0){
  4. return 0;
  5. }
  6. Stack<Integer> stack = new Stack();
  7. for(String str : ops){
  8. if(str.equals("C")){
  9. stack.pop();
  10. }
  11. else if(str.equals("D")){
  12. int top = stack.peek();
  13. stack.push(top*2);
  14. }
  15. else if(str.equals("+")){
  16. int top = stack.pop();
  17. int sec =stack.peek();
  18. stack.push(top);
  19. stack.push(top+sec);
  20. }
  21. else{
  22. int score = Integer.valueOf(str);
  23. stack.push(score);
  24. }
  25. }
  26. int sum = 0;
  27. for (int i : stack){
  28. sum =sum + i;
  29. }
  30. return sum;
  31. }
  32. }

LeetCode - Baseball Game的更多相关文章

  1. [LeetCode] 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算法题

    DFS算法 思想:一直往深处走,直到找到解或者走不下去为止 DFS(dep,...) // dep代表目前DFS的深度 { if (找到解或者走不下去了){ return; } 枚举下种情况,DFS( ...

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

  4. 【LeetCode】682. Baseball Game 解题报告(Python)

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

  5. LeetCode算法题-Baseball Game(Java实现)

    这是悦乐书的第288次更新,第305篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第156题(顺位题号是682).你现在是棒球比赛点记录器.给定一个字符串列表,每个字符串 ...

  6. LeetCode 682 Baseball Game 解题报告

    题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...

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

  8. C#LeetCode刷题之#682-棒球比赛(Baseball Game)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4028 访问. 你现在是棒球比赛记录员. 给定一个字符串列表,每个 ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 自定义xadmin后台首页

    登陆xadmin后台,首页默认是空白,可以自己添加小组件,xadmin一切都是那么美好,但是添加小组件遇到了个大坑,快整了2个礼拜,最终实现想要的界面.初始的页面如图: 本机后台显示这个页面正常,do ...

  2. caffe中全卷积层和全连接层训练参数如何确定

    今天来仔细讲一下卷基层和全连接层训练参数个数如何确定的问题.我们以Mnist为例,首先贴出网络配置文件: name: "LeNet" layer { name: "mni ...

  3. jquery 将一组元素转换成数组

    HTML 代码: <p><b>Values: </b></p> <form> <input type="text" ...

  4. session会话示例

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  5. Docker(3):Dockerfile介绍及简单示例

    Dockerfile 概念 Dockerfile是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像.它们简化了从头到尾的流程并极大的简化了部署工作.Dockerfile从FR ...

  6. 循环神经网络-RNN进阶

    这部分许多内容要类比CNN来进行理解和解释,所以需要对CNN比较熟悉. RNN的特点 1. 权值共享 CNN权值共享,RNN也有权值共享,在入门篇可以看到RNN结构图中,权重使用的是同样的字母 为什么 ...

  7. Sass 混合宏、继承、占位符 详解

    混合宏-声明混合宏如果你的整个网站中有几处小样式类似,比如颜色,字体等,在 Sass 可以使用变量来统一处理,那么这种选择还是不错的.但当你的样式变得越来越复杂,需要重复使用大段的样式时,使用变量就无 ...

  8. 小波学习之一(单层一维离散小波变换DWT的Mallat算法C++和MATLAB实现) ---转载

      1 Mallat算法 离散序列的Mallat算法分解公式如下: 其中,H(n).G(n)分别表示所选取的小波函数对应的低通和高通滤波器的抽头系数序列. 从Mallat算法的分解原理可知,分解后的序 ...

  9. leetcode第40题:组合总和II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  10. day 74 json 和 ajax 的实例

    一 json的定义: json(JavaScript object notation,js对象标记)是一种轻量级的数据交换格式,它基于ecmascript(w3c指定的js规范)的一个子集,采用完全独 ...