力扣682.棒球比赛

力扣682.棒球比赛

  • 数组模拟栈记录分数

  •   class Solution {
      public:
          int calPoints(vector<string>& ops) {
              int res=0;
              vector<int> points;
              for(auto &op:ops)
              {
                  int n = points.size();
                  char c = op[0];
                  if(c == '+')
                  {
                      res += points[n-1] + points[n-2];
                      points.push_back(points[n-1]+points[n-2]);
                  }
                  else if(c == 'D')
                  {
                      res += points[n-1]*2;
                      points.push_back(points[n-1]*2);
                  }
                  else if(c == 'C')
                  {
                      res -= points[n-1];
                      points.pop_back();
                  }
                  else
                  {
                      res += stoi(op);
                      points.push_back(stoi(op));
                  }
              }
              return res;
          }
      };
    

相关推荐

  1. 682.棒球比赛

    2024-07-11 20:48:07       18 阅读
  2. leetcode08-棒球比赛

    2024-07-11 20:48:07       60 阅读
  3. [题解] 684. 冗余连接

    2024-07-11 20:48:07       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 20:48:07       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 20:48:07       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 20:48:07       58 阅读
  4. Python语言-面向对象

    2024-07-11 20:48:07       69 阅读

热门阅读

  1. STM32学习历程(day4)

    2024-07-11 20:48:07       20 阅读
  2. C# 装饰器模式(Decorator Pattern)

    2024-07-11 20:48:07       20 阅读
  3. 代码随想录-DAY⑦-字符串——leetcode 344 | 541 | 151

    2024-07-11 20:48:07       21 阅读
  4. FastAPI+SQLAlchemy数据库连接

    2024-07-11 20:48:07       19 阅读
  5. 关于vue监听数组

    2024-07-11 20:48:07       18 阅读
  6. SQL 自定义函数

    2024-07-11 20:48:07       22 阅读
  7. linux内核访问读写用户层文件方法

    2024-07-11 20:48:07       21 阅读
  8. RK3568平台开发系列讲解(网络篇)netfilter框架

    2024-07-11 20:48:07       19 阅读
  9. Netty服务端接收TCP链接数据

    2024-07-11 20:48:07       16 阅读
  10. 【面试题】Golang (第一篇)

    2024-07-11 20:48:07       22 阅读