LeetCode - 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 two valid round's points.
"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.
"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.
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做,注意stack里面存的元元素是什么。
class Solution {
    public int calPoints(String[] ops) {
        if(ops == null || ops.length == 0){
            return 0;
        }
        Stack<Integer> stack = new Stack();
        for(String str : ops){
            if(str.equals("C")){
                stack.pop();
            }
            else if(str.equals("D")){
                int top = stack.peek();
                stack.push(top*2);
            }
            else if(str.equals("+")){
                int top = stack.pop();
                int sec =stack.peek();
                stack.push(top);
                stack.push(top+sec);
            }
            else{
                int score = Integer.valueOf(str);
                stack.push(score);
            }
        }
        int sum  = 0;
        for (int i : stack){
            sum =sum + i;
        }
        return sum;
    }
}
LeetCode - Baseball Game的更多相关文章
- [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算法题
		
DFS算法 思想:一直往深处走,直到找到解或者走不下去为止 DFS(dep,...) // dep代表目前DFS的深度 { if (找到解或者走不下去了){ return; } 枚举下种情况,DFS( ...
 - [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 ...
 - 【LeetCode】682. Baseball Game 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈模拟 日期 题目地址:https://leet ...
 - LeetCode算法题-Baseball Game(Java实现)
		
这是悦乐书的第288次更新,第305篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第156题(顺位题号是682).你现在是棒球比赛点记录器.给定一个字符串列表,每个字符串 ...
 - 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&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 ...
 - C#LeetCode刷题之#682-棒球比赛(Baseball Game)
		
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4028 访问. 你现在是棒球比赛记录员. 给定一个字符串列表,每个 ...
 - Swift LeetCode 目录 | Catalog
		
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
 
随机推荐
- 遍历所有子物体中renderer(渲染器)中的material(材质)
			
//得到所有可渲染的子物体Renderer[] rds = transform.GetComponentsInChildren<Renderer>();//逐一遍历他的子物体中的Rende ...
 - jenkins部署java项目在本地(三)
			
(1)新建maven构建的java项目 pom.xml的配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...
 - day22 模块_1
			
核能来袭--模块 1.简单了解模块 2.Collections 3.Time模块 4.functools 一.初识模块 其实之前写的每一个PY文件都是一个模块 还有一些我们一直在使用的模块 buil ...
 - L1-054 福到了
			
“福”字倒着贴,寓意“福到”.不论到底算不算民俗,本题且请你编写程序,把各种汉字倒过来输出.这里要处理的每个汉字是由一个 N × N 的网格组成的,网格中的元素或者为字符 @ 或者为空格.而倒过来的汉 ...
 - tomcat的安装及配置
			
1.首先进tomcat官网下载zip压缩文件:http://tomcat.apache.org/download-90.cgi 2.解压缩到指定文件压(后面配置环境变量会用到) 3.配置环境变量 4. ...
 - L308 New brain cells made throughout life
			
People keep making new brain cells throughout their lives (well at least until the age of 97), accor ...
 - day 46 前端基础 基本框架
			
注意一点 使用绝对路径的时候 在pxm里 打开显示不了图片 可以直接找到那个实际的网页去打开 还可能是图片的格式尽量用jpg一 详细解释 <!DOCTYPE html>声明为HTML5文档 ...
 - 5--Selenium环境准备--firefox与geckodriver
			
selenium2时打开firefox浏览器是不需要安装firefoxdriver的,但是selenium3不支持向前支持火狐浏览器了,40以后版本的火狐,运行会出现问题. 1.下载geckodriv ...
 - 2018最新APP Android UI设计规范
			
设计稿尺寸:从目前市场主流设备尺寸来看,我们要用 1080 x 1920 PX 来做安卓设计稿尺寸. 以1080x1920px作为设计稿标准尺寸的原由: 从中间尺寸向上和向下适配的时候界面调整的幅度 ...
 - Java的Annotation标签
			
只需要简单的使用Java的Annotation标签即可将标准的Java方法发布成Web Service,但不是所有的Java类都可以发布成Web Service.Java类若要成为一个实现了Web S ...