每周一算法2017.12.22

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.

描述:

棒球比赛 你现在是一个棒球游戏点记录器。

给定一个字符串列表,每个字符串可以是以下4种类型之一:

整数(一个圆的分数):直接代表你在这个回合中得到的点数。 “+”(一轮得分):代表你在这一轮中得到的分数是最后两个有效回合的分数之和。 “D”(一轮得分):代表你在这一轮中得到的分数是最后一轮有效点的双倍数据。 “C”(一个操作,不是圆的分数):表示你得到的最后一轮有效点无效,应该去掉。 每一轮的操作都是永久性的,对回合前和回合后都有影响。

你需要在所有回合中返回你能得到的分数的总和。

例1: 输入:[ 5 ]、“2”、“C”、“D”、“+” 输出:30 解释: 第1回合:你可以得到5分。总和是:5。 第2回合:你可以得到2分。总和是:7。 操作1:第2轮的数据无效。总和是:5。 第3轮:你可以得到10分(轮2的数据已经被删除)。总和是:15。 第4回合:你可以得到5 + 10 = 15分。总和是:30。 例2: 输入:[ 5 ],“- 2”,“4”,“C”,“D”,“9”,“+”,“+” 输出:27 解释: 第1回合:你可以得到5分。总和是:5。 第2回合:你可以得到2分。总和是:3。 第3回合:你可以得到4分。总和是:7。 操作1:第3轮的数据无效。总和是:3。 第4回合:你可以得到4分(第3轮的数据被删除)。总和是:- 1。 第5回合:你可以得到9分。总和是:8。 回合6:你可以得到- 4 + 9 = 5分。总和是13。 第7回合:你可以得到9 + 5 = 14分。总和是27。 注: 输入列表的大小将介于1和1000之间。 列表中表示的每个整数将介于30000和30000之间。

c++:

#include <iostream>
using namespace std;  
int baseballGamePointRecorder(vector<string>& ops);  
int main() {

    vector<string> scoreArray{ "5","-2","4","C","D","9","+","+"};
    int sum = baseballGamePointRecorder(scoreArray);
    std::cout << sum;
}

int baseballGamePointRecorder(vector<string>& scoreArray)  
{
    vector<int> tempScore;
    int sum = 0;

    for(int i = 0;i<scoreArray.size();i++){
        int score = 0;
        if (scoreArray[i] == "+")
            sum += score = tempScore.back() + tempScore[tempScore.size() - 2];
        else if (scoreArray[i] == "C"){
            sum -= tempScore.back();
            tempScore.pop_back();
            continue;
        }
        else if (scoreArray[i] == "D")
            sum += score = tempScore.back() * 2;
        else
            sum+=score=stoi(scoreArray[i]);
        tempScore.push_back(score);
    }
    return sum;
}

Java:

public class Main {  
    public static void main(String[] args) {
    String[] scoreData = new String[] {"5","-2","4","C","D","9","+","+"};
    int sum = baseballGamePointRecorder(scoreData);
    System.out.println(sum);
}


public static int baseballGamePointRecorder(String[] scoreArray) {  
    Stack<Integer> stack = new Stack();
    for(String scoreString : scoreArray) {
        if (scoreString.equals("+")) {
            int top = stack.pop();
            int new1 = top + stack.peek();
            stack.push(top);
            stack.push(new1);
        } else if (scoreString.equals("C")) {
            stack.pop();
        } else if (scoreString.equals("D")) {
            stack.push(2 * stack.peek());
        } else {
            stack.push(Integer.valueOf(scoreString));
        }
    }

    int sum = 0;
    for(int score : stack) sum += score;
    return sum;
    }

}

Swift:

func  baseballGamePointRecorder (scoreArray:Array<String>) {

    var pathStack = [Int]()
    _ = String()
    for scoreString in scoreArray {
        if scoreString == "+" {
            let top = pathStack.popLast();
            let new1 = top! + pathStack.last!;
            pathStack.append(top!);
            pathStack.append(new1);

        }else if scoreString == "C" {
            pathStack.popLast();

        }else if scoreString == "D" {
            pathStack.append(2 * pathStack.last!);

        }else {
            let pointString: String = scoreString
            pathStack.append(Int(pointString)!);
        }
    }

    var sum = 0;
    _ = Int()
    for score in pathStack {
        sum += score;
    }
    print("score: \(sum)")

}

var scoreData =  ["5","-2","4","C","D","9","+","+"]  
baseballGamePointRecorder(scoreArray: scoreData)

本题来自:leetCode

comments powered by Disqus