成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Baseball Game

newtrek / 1750人閱讀

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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/71379.html

相關(guān)文章

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

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

    wzyplus 評(píng)論0 收藏0
  • [LintCode/LeetCode] Jump Game I & II

    摘要:建立動(dòng)規(guī)數(shù)組,表示從起點(diǎn)處到達(dá)該點(diǎn)的可能性。循環(huán)結(jié)束后,數(shù)組對(duì)所有點(diǎn)作為終點(diǎn)的可能性都進(jìn)行了賦值。和的不同在于找到最少的步數(shù)。此時(shí)的一定是滿足條件的最小的,所以一定是最優(yōu)解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...

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

    摘要:添加元素到數(shù)組合并兩個(gè)數(shù)組錯(cuò)誤方法應(yīng)該用方法,將被的數(shù)組當(dāng)成參數(shù)數(shù)組。會(huì)改變數(shù)組,返回最新屬性,占用內(nèi)存較少。 一、Array.prototype.concat() concat方法將創(chuàng)建一個(gè)新的數(shù)組,然后將調(diào)用它的對(duì)象(this指向的對(duì)象)中的元素以及所有參數(shù)中的數(shù)組類型的參數(shù)中的元素以及非數(shù)組類型的參數(shù)本身按照順序放入這個(gè)新數(shù)組,并返回該數(shù)組。concat方法并不修改調(diào)用它的對(duì)象...

    econi 評(píng)論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 評(píng)論0 收藏0
  • js數(shù)組常用的一些方法

    摘要:如果被引用的對(duì)象發(fā)生改變,則改變將反應(yīng)到新的和原來(lái)的數(shù)組中對(duì)于字符串和數(shù)字來(lái)說(shuō)不是和對(duì)象,會(huì)拷貝字符串和數(shù)字到新的數(shù)組里。在一個(gè)數(shù)組里修改這些字符串或數(shù)字,不會(huì)影響另一個(gè)數(shù)組。 (1) arr.length => 返回一個(gè)數(shù)組中的元素個(gè)數(shù)(數(shù)組屬性) var numbers = [1,2,3,4,5]; numbers.length; // 5 (2) arr.indexOf(sear...

    wendux 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<