首页 > 其他分享 >LWC 51:682. Baseball Game

LWC 51:682. Baseball Game

时间:2023-07-10 16:32:53浏览次数:53  
标签:get int sum Game points Baseball ans LWC round


LWC 51:682. Baseball Game

传送门:682. Baseball Game

Problem:

You’re now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round’s score): Directly represents the number of points you get in this round.
  2. “+” (one round’s score): Represents that the points you get in this round are the sum of the last two valid round’s points.
  3. “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.
  4. “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.

思路:
straightforward,题意即思路。

代码如下:

public int calPoints(String[] ops) {
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < ops.length; ++i) {
            String val = ops[i];
            if (val.equals("C")) {
                ans.remove(ans.size() - 1);
            }
            else if (val.equals("D")) {
                int last = ans.get(ans.size() - 1);
                ans.add(last * 2);
            }
            else if (val.equals("+")) {
                int last = ans.get(ans.size() - 1);
                int prev = ans.get(ans.size() - 2);
                ans.add(last + prev);
            }
            else {
                int num = Integer.parseInt(val);
                ans.add(num);
            }
        }

        int sum = 0;
        for (int i = 0; i < ans.size(); ++i) {
            sum += ans.get(i);
        }
        return sum;
    }

数组的长度其实是已知的,所以直接用指针动态增减,代码如下:

public int calPoints(String[] ops) {
        int n = ops.length;
        int[] list = new int[n + 1];
        int tot = 0;
        for (int i = 0; i < n; ++i) {
            String val = ops[i];
            if (val.equals("C")) {
                tot--;
            }
            else if (val.equals("D")) {
                int lst = list[tot - 1];
                list[tot++] = lst * 2;
            }
            else if (val.equals("+")) {
                int lst = list[tot - 1];
                int prv = list[tot - 2];
                list[tot++] = lst + prv;
            }
            else {
                int num = Integer.parseInt(val);
                list[tot++] = num;
            }
        }

        int sum = 0;
        for (int i = 0; i < tot; ++i) {
            sum += list[i];
        }

        return sum;
    }


标签:get,int,sum,Game,points,Baseball,ans,LWC,round
From: https://blog.51cto.com/u_16184402/6678321

相关文章

  • LWC 50:679. 24 Game
    LWC50:679.24Game传送门:679.24GameProblem:Youhave4cardseachcontaininganumberfrom1to9.Youneedtojudgewhethertheycouldoperatedthrough*,/,+,-,(,)togetthevalueof24.Example1:Input:[4,1,8,7]Output:TrueExplanation:(8-4)......
  • LWC 50:678. Valid Parenthesis String
    LWC50:678.ValidParenthesisString传送门:678.ValidParenthesisStringProblem:Givenastringcontainingonlythreetypesofcharacters:‘(‘,‘)’and‘*’,writeafunctiontocheckwhetherthisstringisvalid.Wedefinethevalidityofastringbythese......
  • LWC 50:677. Map Sum Pairs
    LWC50:677.MapSumPairs传送门:677.MapSumPairsProblem:ImplementaMapSumclasswithinsert,andsummethods.Forthemethodinsert,you’llbegivenapairof(string,integer).Thestringrepresentsthekeyandtheintegerrepresentsthevalue.Ifthekey......
  • LWC 54:697. Degree of an Array
    LWC54:697.DegreeofanArray传送门:697.DegreeofanArrayProblem:Givenanon-emptyarrayofnon-negativeintegersnums,thedegreeofthisarrayisdefinedasthemaximumfrequencyofanyoneofitselements.Yourtaskistofindthesmallestpossibleleng......
  • CF842E Nikita and game 题解
    题意一棵树初始只有一个编号为1的根结点。\(n\)次操作,每次新增一个点作为\(p_i\)的子结点,询问更新后有多少点可以作为树直径的端点。\(n\le3\times10^5\)。题解以下\(dist(x,y)\)表示点\(x\)与点\(y\)在树上的距离。不难发现若干条直径必然叠合于至少一点,任选这......
  • Building a Dice Game using JavaScript Javascript构建一个dice game 项目
    WewillbebuildingaDiceGameProjectusingHTML,CSS,andJavaScript.TheDiceGameisbasedonatwo-player.Bothplayersrollthediceandtheplayerwhogetsthehighestphasevaluewillwinthegame.ImagesofDicePhases: Thelistofdicephasesi......
  • CF1411G No Game No Life
    猜它是一个multi-sg,只用算出每个位置的sg值。不过注意到这是一个图,你要求mex肯定不会太大,毛咕咕一下不会超过\(\sqrt{m}\)。并且根据均摊,你求mex的复杂度是\(O(m)\)的。接下来相当于你有一个数\(v\)每次选一个点异或上它的sg值,求最后是\(0\)的概率。枚举这个过程......
  • xss漏洞攻击复现(xssgame靶场通关)
     这篇文章简单的介绍下xssgame的通关方法,从名字可以看出,xssgame就是针对xss攻击进行专门的漏洞复现,由易到难。 链接:https://pan.baidu.com/s/1F9I7iBdu7MPLLvegM5kAQg 提取码:469c 这是xssgame的安装包,将它放到phpstudy/WWW文件夹下访问即可 第一关 这一关没有任......
  • 关于游戏开发及Gamejam的一些尝试
    阅读笔记《01游戏设计》01游戏开发的意义文创意义1.时刻牢记,我们设计的终究是一款游戏,它首先需要对玩家的体验负责。所谓教育意义和弘扬文化只是在游戏体验完善的基础上,和题材风格叠加产生的效果。一开始就抱着“使命感”去设计游戏,反而不会制作出优秀的作品,只有优秀的游戏......
  • unreal engine 5.2 c++ 自定义gameplay
    1.新建c++工程2.打开worldsetting3.修改默认GamePlay4.依次新建对应GamePlay替换默认GamePlayDefaultPawnHUDPlayerControllerGameStatePlayerStateSpectatorPawn5.添加AhellogpGameModeBase默认构造函数#include"hellogpGameModeBase.h"#include......