Leetcode 682. Baseball Game

版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/83624920

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Baseball Game

2. Solution

class Solution {
public:
    int calPoints(vector<string>& ops) {
        int score = 0;
        int sum = 0;
        vector<int> scores;
        for(int i = 0; i < ops.size(); i++) {
            if(ops[i] == "C") {
                sum -= scores.back();
                scores.pop_back();
            }
            else if (ops[i] == "D") {
                score = 2 * scores.back();
                sum += score;
                scores.push_back(score);
            }
            else if ((ops[i] == "+")) {
                score = scores.back() + scores[scores.size() - 2];
                sum += score;
                scores.push_back(score);
            }
            else {
                score = stoi(ops[i]);
                sum += score;
                scores.push_back(score);
            }
        }
        return sum;
    }
};

Reference

  1. https://leetcode.com/problems/baseball-game/description/
阅读更多

扫码向博主提问

SnailTyan

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 深度学习
  • PyTorch
  • OCR
  • Docker
  • Caffe
去开通我的Chat快问

没有更多推荐了,返回首页