Java实现 LeetCode 682 棒球比赛(暴力)
682. 棒球比赛
你现在是棒球比赛记录员。
给定一个字符串列表,每个字符串可以是以下四种类型之一:
1.整数(一轮的得分):直接表示您在本轮中获得的积分数。
2. “+”(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。
3. “D”(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。
4. “C”(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应该被移除。
每一轮的操作都是永久性的,可能会对前一轮和后一轮产生影响。
你需要返回你在所有回合中得分的总和。
示例 1:
输入: ["5","2","C","D","+"]
输出: 30
解释:
第1轮:你可以得到5分。总和是:5。
第2轮:你可以得到2分。总和是:7。
操作1:第2轮的数据无效。总和是:5。
第3轮:你可以得到10分(第2轮的数据已被删除)。总数是:15。
第4轮:你可以得到5 + 10 = 15分。总数是:30。
示例 2:
输入: ["5","-2","4","C","D","9","+","+"]
输出: 27
解释:
第1轮:你可以得到5分。总和是:5。
第2轮:你可以得到-2分。总数是:3。
第3轮:你可以得到4分。总和是:7。
操作1:第3轮的数据无效。总数是:3。
第4轮:你可以得到-4分(第三轮的数据已被删除)。总和是:-1。
第5轮:你可以得到9分。总数是:8。
第6轮:你可以得到-4 + 9 = 5分。总数是13。
第7轮:你可以得到9 + 5 = 14分。总数是27。
注意:
输入列表的大小将介于1和1000之间。
列表中的每个整数都将介于-30000和30000之间。
class Solution {
public int calPoints(String[] ops) {
int[] arr = new int[ops.length];
int i=0;
for(String s:ops){
switch (s){
case "+":arr[i]=arr[i-1]+arr[i-2];i++;break;
case "D":arr[i]=2*arr[i-1];i++;break;
case "C":arr[i-1]=0;i--;break;
default:
arr[i]=Integer.valueOf(s);
i++;
}
}
int sum=0;
for (int j = 0; j <arr.length ; j++) {
sum+=arr[j];
}
return sum;
}
}
Java实现 LeetCode 682 棒球比赛(暴力)的更多相关文章
- LeetCode:棒球比赛【682】
LeetCode:棒球比赛[682] 题目描述 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. " ...
- 【Leetcode】【简单】【682棒球比赛】【JavaScript】
题目 682. 棒球比赛 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. "+"(一轮的 ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [Swift]LeetCode682. 棒球比赛 | Baseball Game
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
随机推荐
- 手写一个简易的多周期 MIPS CPU
一点前言 多周期 CPU 相比单周期 CPU 以及流水线 CPU 实现来说其实写起来要麻烦那么一些,但是相对于流水线 CPU 和单周期 CPU 而言,多周期 CPU 除了能提升主频之外似乎并没有什么卵 ...
- 使用jquery实现的自适应导航
话不多说,直接晒代码 <div class="headering"> <div class="header-top"> <div ...
- 编译nginx的时候报错 需要安装PCRE
./configure --prefix=/mynginx/ 本地编译nginx的时候 报错 提示需要安装PCRE 错误信息: ./configure: error: the HTTP rewrite ...
- java学习笔记之原型模式及深浅拷贝
一.原型模式的基本介绍 在聊原型模式之前,我们来思考一个小问题,传统的方式我们是如何克隆对象呢? 那我们以多利羊(Sheep)为例,来说明上述这个问题,具体代码见下面: 多利羊(Sheep) publ ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 完善与美化,Swagger登场
上一篇文章(https://www.cnblogs.com/meowv/p/12896898.html)已经成功将博客项目跑起来了,那么本篇主要是将之前遗留的问题解决,现在的代码看起来可能还是比较混乱 ...
- 关于web标准
从我去年接触前端,到现在,我似乎都没有特意去研究过web标准.我只知道传统上推崇结构样式行为分离,js.css.html各司其职, 不推荐在文档的节点上写类似<p onclick=“fn()”& ...
- jQuery的面试题
1.$的原理 答案: 1)$("选择器")是先查找DOM元素,再将DOM元素放入jQuery对象中 其中自带优化: 如果选择器是#id,则自动调用getElementById 如果 ...
- 5.4 Go 闭包
5.4 Go 闭包 闭包(closure):是由一个函数和其相关的引用环境组合的一个整体.(闭包=函数+引用环境) package main import ( "fmt" ) // ...
- HTTP响应格式
HTTP响应格式
- day09作业01用户登录与验证
import timeLoginTime = time.asctime( time.localtime(time.time()) )print ("time %s" % Login ...