1. 程式人生 > 其它 >mongodb常用查詢語句

mongodb常用查詢語句

你現在是一場採用特殊賽制棒球比賽的記錄員。這場比賽由若干回合組成,過去幾回合的得分可能會影響以後幾回合的得分。

比賽開始時,記錄是空白的。你會得到一個記錄操作的字串列表 ops,其中 ops[i] 是你需要記錄的第 i 項操作,ops 遵循下述規則:

整數 x - 表示本回合新獲得分數 x
"+" - 表示本回合新獲得的得分是前兩次得分的總和。題目資料保證記錄此操作時前面總是存在兩個有效的分數。
"D" - 表示本回合新獲得的得分是前一次得分的兩倍。題目資料保證記錄此操作時前面總是存在一個有效的分數。
"C" - 表示前一次得分無效,將其從記錄中移除。題目資料保證記錄此操作時前面總是存在一個有效的分數。
請你返回記錄中所有得分的總和。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/baseball-game
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

import java.util.Stack;

class Solution {
    public int calPoints(String[] ops) {
        Stack<Integer> stack = new Stack<>();
        for (String op : ops) {
            if ("+".equals(op)) {
                int num = stack.pop();
                int sum = stack.peek() + num;
                stack.push(num);
                stack.push(sum);
            } else if ("D".equals(op)) {
                stack.push(stack.peek() * 2);
            } else if ("C".equals(op)) {
                stack.pop();
            } else {
                stack.push(Integer.parseInt(op));
            }
        }

        return stack.stream().reduce(0, Integer::sum);
    }
}
心之所向,素履以往 生如逆旅,一葦以航