682-Baseball Game

Title description

You are now the recorder of a baseball game with a special format. This game consists of several rounds, and the scoring in the past few rounds may affect the scoring in the next few rounds.

When the game started, the record was blank. You will get a string list ops that records operations, where ops[i] is the ith operation you need to record, ops follows the following rules:

  1. Integer x-represents the new score x obtained in this round
  2. "+"-Indicates that the new score obtained in this round is the sum of the previous two scores. The item data guarantees that there are always two valid scores before this operation is recorded.
  3. "D"-Indicates that the new score obtained in this round is twice the previous score. The item data guarantees that there is always a valid score in front of the record of this operation.
  4. "C"-indicates that the previous score is invalid, and it is removed from the record. The item data guarantees that there is always a valid score in front of the record of this operation.
    Please return the sum of all the scores in the record.

Insert picture description here

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/baseball-game
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

answer

  1. Solution 1
    Use if judgment sentence, which takes a long time
int calPoints(char ** ops, int opsSize){
    
    
    int score[1000];
    int top=-1;
    int sum=0;

    for(int i=0;i<opsSize;i++)
    {
    
    
        if(ops[i][0]=='C')
        {
    
    
            top--;
        }
        else if(ops[i][0]=='D')
        {
    
    
            top++;
            score[top]=score[top-1]*2;
        }
        else if(ops[i][0]=='+')
        {
    
    
            top++;
            score[top]=score[top-1]+score[top-2];
        }
        else
        {
    
    
            top++;
            score[top]=0;
            if(ops[i][0]=='-')
            {
    
    
                for(int j=1;ops[i][j]!='\0';j++)
                {
    
    
                    score[top]*=10;
                    score[top]-=(int)ops[i][j]-48;
                }
            }
            else
            {
    
    
                for(int j=0;ops[i][j]!='\0';j++)
                {
    
    
                    score[top]*=10;
                    score[top]+=(int)ops[i][j]-48;
                }
            }
            
        }
    }

    for(int i=0;i<=top;i++)
    {
    
    
        sum+=score[i];
    }

    return sum;
}
  1. Improve the
    use of switch, the time is shortened
int calPoints(char ** ops, int opsSize){
    
    
    int score[1000];
    int top=-1;
    int sum=0;

    for(int i=0;i<opsSize;i++)
    {
    
    
        switch (ops[i][0])
        {
    
    
            case 'C':
                top--;
                break;
            case 'D':
                top++;
                score[top]=score[top-1]*2;
                break;
            case '+':
                top++;
                score[top]=score[top-1]+score[top-2];
                break;
            default:
                top++;
                score[top]=0;
                if(ops[i][0]=='-')
                {
    
    
                    for(int j=1;ops[i][j]!='\0';j++)
                    {
    
    
                        score[top]*=10;
                        score[top]-=(int)ops[i][j]-48;
                    }
                }
                else
                {
    
    
                    for(int j=0;ops[i][j]!='\0';j++)
                    {
    
    
                        score[top]*=10;
                        score[top]+=(int)ops[i][j]-48;
                    }
                }
                break;
        }
    }

    for(int i=0;i<=top;i++)
    {
    
    
        sum+=score[i];
    }

    return sum;
}
  1. Improvement 2
    1. Use the C language library function atoi to convert a string integer into an integer integer, and the space used is slightly increased
    2. Use malloc function to allocate memory according to string length
int calPoints(char ** ops, int opsSize){
    
    
    int* score=(int *)malloc(opsSize*sizeof(int));
    int top=-1;
    int sum=0;

    for(int i=0;i<opsSize;i++)
    {
    
    
        switch (ops[i][0])
        {
    
    
            case 'C':
                top--;
                break;
            case 'D':
                top++;
                score[top]=score[top-1]*2;
                break;
            case '+':
                top++;
                score[top]=score[top-1]+score[top-2];
                break;
            default:
                top++;
                score[top]=atoi(ops[i]);          
                break;
        }
    }

    for(int i=0;i<=top;i++)
    {
    
    
        sum+=score[i];
    }

    return sum;
}

Guess you like

Origin blog.csdn.net/qq_36439722/article/details/112227165