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.

分析

class Solution {
    public int calPoints(String[] ops) {
        int last1 = 0, last2 = 0, sum = 0;
        Stack<Integer> stk = new Stack<Integer>();
        for(int i = 0; i < ops.length; i++) {
            String curr = ops[i];
            Character c = curr.charAt(0);
            switch(c) {
                case '+':
                    last1 = stk.pop();
                    last2 = stk.peek();
                    stk.push(last1);
                    stk.push(last1 + last2);
                    sum += last1 + last2;
                    break;
                case 'D':
                    int doubled = stk.peek() * 2;
                    stk.push(doubled);
                    sum += doubled;
                    break;
                case 'C':
                    sum = sum - stk.pop();
                    break;
                default:
                    int num = Integer.parseInt(curr);
                    stk.push(num);
                    sum = sum + num;
                    break;
            }
        }
        return sum;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市林螃,隨后出現(xiàn)的幾起案子奕删,更是在濱河造成了極大的恐慌,老刑警劉巖疗认,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件完残,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡横漏,警方通過(guò)查閱死者的電腦和手機(jī)谨设,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)缎浇,“玉大人扎拣,你說(shuō)我怎么就攤上這事』罚” “怎么了鹏秋?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)亡笑。 經(jīng)常有香客問我侣夷,道長(zhǎng),這世上最難降的妖魔是什么仑乌? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任百拓,我火速辦了婚禮,結(jié)果婚禮上晰甚,老公的妹妹穿的比我還像新娘衙传。我一直安慰自己,他們只是感情好厕九,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布蓖捶。 她就那樣靜靜地躺著,像睡著了一般扁远。 火紅的嫁衣襯著肌膚如雪俊鱼。 梳的紋絲不亂的頭發(fā)上刻像,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音并闲,去河邊找鬼细睡。 笑死,一個(gè)胖子當(dāng)著我的面吹牛帝火,可吹牛的內(nèi)容都是我干的溜徙。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼犀填,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼蠢壹!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起宏浩,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤知残,失蹤者是張志新(化名)和其女友劉穎靠瞎,沒想到半個(gè)月后比庄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡乏盐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年佳窑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片父能。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡神凑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出何吝,到底是詐尸還是另有隱情溉委,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布爱榕,位于F島的核電站瓣喊,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏黔酥。R本人自食惡果不足惜藻三,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望跪者。 院中可真熱鬧棵帽,春花似錦、人聲如沸渣玲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)忘衍。三九已至逾苫,卻和暖如春指巡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背隶垮。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工藻雪, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人狸吞。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓勉耀,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親蹋偏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子便斥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容