Problem
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.
Example1:
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.
Example2:
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.
public class Solution { /** * @param ops: the list of operations * @return: the sum of the points you could get in all the rounds */ public int calPoints(String[] ops) { // Write your code here ListscoreRecord = new ArrayList<>(); int score = 0; for (String s: ops) { int size = scoreRecord.size(); if (isNumeric(s)) { score += Integer.valueOf(s); scoreRecord.add(Integer.valueOf(s)); } else if (s.equals("C")) { score -= scoreRecord.get(size-1); scoreRecord.remove(size-1); } else if (s.equals("D")) { int roundScore = 2*scoreRecord.get(size-1); score += roundScore; scoreRecord.add(roundScore); } else if (s.equals("+")) { int prevOne = size >= 1 ? scoreRecord.get(size-1) : 0; int prevTwo = size >= 2 ? scoreRecord.get(size-2) : 0; int roundScore = prevOne + prevTwo; score += roundScore; scoreRecord.add(roundScore); } else { continue; } } return score; } public boolean isNumeric(String s) { return s != null && s.matches("[-+]?d*.?d+"); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/71379.html
摘要:題目鏈接題目分析給定一個字符串?dāng)?shù)組,每一個字符串有以下形式數(shù)字。直接計算得分。。代表上一輪分數(shù)無效。思路這題沒什么好說的了。用區(qū)分各種情況,進行相應(yīng)處理即可。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 682. Baseball Game 題目鏈接 682. Baseball Game 題目分析 給定一個字符串?dāng)?shù)組,每一個字符串有以下形式: 數(shù)字。直接計算得分。 +。代表本輪...
摘要:建立動規(guī)數(shù)組,表示從起點處到達該點的可能性。循環(huán)結(jié)束后,數(shù)組對所有點作為終點的可能性都進行了賦值。和的不同在于找到最少的步數(shù)。此時的一定是滿足條件的最小的,所以一定是最優(yōu)解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...
摘要:添加元素到數(shù)組合并兩個數(shù)組錯誤方法應(yīng)該用方法,將被的數(shù)組當(dāng)成參數(shù)數(shù)組。會改變數(shù)組,返回最新屬性,占用內(nèi)存較少。 一、Array.prototype.concat() concat方法將創(chuàng)建一個新的數(shù)組,然后將調(diào)用它的對象(this指向的對象)中的元素以及所有參數(shù)中的數(shù)組類型的參數(shù)中的元素以及非數(shù)組類型的參數(shù)本身按照順序放入這個新數(shù)組,并返回該數(shù)組。concat方法并不修改調(diào)用它的對象...
This morning , I took my first leetcode online.However, that is far beyond what I expected . I didn’t make it to finish in 1 hour and 30 minutes. But via solving the 1st problem , which is marked ea...
摘要:如果被引用的對象發(fā)生改變,則改變將反應(yīng)到新的和原來的數(shù)組中對于字符串和數(shù)字來說不是和對象,會拷貝字符串和數(shù)字到新的數(shù)組里。在一個數(shù)組里修改這些字符串或數(shù)字,不會影響另一個數(shù)組。 (1) arr.length => 返回一個數(shù)組中的元素個數(shù)(數(shù)組屬性) var numbers = [1,2,3,4,5]; numbers.length; // 5 (2) arr.indexOf(sear...
閱讀 1292·2023-04-26 01:03
閱讀 1907·2021-11-23 09:51
閱讀 3299·2021-11-22 15:24
閱讀 2663·2021-09-22 15:18
閱讀 1010·2019-08-30 15:55
閱讀 3458·2019-08-30 15:54
閱讀 2234·2019-08-30 15:53
閱讀 2387·2019-08-30 15:44