Java Solution beat 80%


  • 0
    L

    class Solution {
    public int calPoints(String[] ops) {

        int sum=0;
        int count=0;
        ArrayList<Integer> roundRecords=new ArrayList<Integer>();
        
        for(int i=0;i<ops.length;i++)
        {
            if(ops[i].equals("+"))
            {
                roundRecords.add(roundRecords.get(count-1)+roundRecords.get(count-2));
                sum+=roundRecords.get(count);
                count++;
            }
            else if(ops[i].equals("D"))
            {
                roundRecords.add(roundRecords.get(count-1)*2);
                sum+=roundRecords.get(count);
                count++;
            }
           else if(ops[i].equals("C"))
            {
                sum-=roundRecords.get(--count);
                roundRecords.remove(count);
               
            }
            else
            {
                roundRecords.add(Integer.parseInt(ops[i]));
                sum+=roundRecords.get(count);
                count++;                
            }
            
        }
        
        return sum;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.