国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode] Baseball Game

newtrek / 1745人閱讀

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.

Example

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.

Solution

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
        List scoreRecord = 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

相關(guān)文章

  • Leetcode PHP題解--D37 682. Baseball Game

    摘要:題目鏈接題目分析給定一個字符串?dāng)?shù)組,每一個字符串有以下形式數(shù)字。直接計算得分。。代表上一輪分數(shù)無效。思路這題沒什么好說的了。用區(qū)分各種情況,進行相應(yīng)處理即可。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 682. Baseball Game 題目鏈接 682. Baseball Game 題目分析 給定一個字符串?dāng)?shù)組,每一個字符串有以下形式: 數(shù)字。直接計算得分。 +。代表本輪...

    wzyplus 評論0 收藏0
  • [LintCode/LeetCode] Jump Game I & II

    摘要:建立動規(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...

    rose 評論0 收藏0
  • (JavaScript) 合并數(shù)組的方法

    摘要:添加元素到數(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)用它的對象...

    econi 評論0 收藏0
  • A flaw of java knowledge found by taking leetcode

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

    impig33 評論0 收藏0
  • js數(shù)組常用的一些方法

    摘要:如果被引用的對象發(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...

    wendux 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<