[C++] Clean Code


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

  • 0

    I wrote the following code snippet for this question:

    class Solution {
    public:
        int calPoints(vector<string>& ops) {
            if(ops.empty())
                return 0;
            
            long long int lValid=0, l2lValid=0, l3lValid=0, prev=0, sum=0, temp=0;
            for(string s : ops) {
                if(s=="D") {
                    sum+=(2*lValid);
                    l3lValid=l2lValid;
                    l2lValid=lValid;
                    lValid*=2;
                }                
                else {
                    if(s=="C") {
                        sum-=lValid;
                        lValid=l2lValid;
                        l2lValid=l3lValid;
                    }
                    else {
                        if(s=="+") {
                            temp=lValid+l2lValid;
                            l3lValid=l2lValid;
                            l2lValid=lValid;
                            lValid=temp;
                            sum+=temp;
                        }
                        else {
                            l3lValid=l2lValid;
                            l2lValid=lValid;
                            lValid=stoi(s);
                            sum+=lValid;
                        }
                    }
                }
            }
            
            return sum;
        }
    };
    

    For the input:

    ["15483","-20523","C","C","9061","26083","+","C","4702","+","D","10260","-525","C","+","15023","-20005","-1647","C","-12853","20706","D","-21983","24892","10570","D","1215","D","D","+","3854","20505","C","D","-18850","-2170","27914","-26175","+","3188","+","+","21804","D","+","-847","D","26184","14945","C","D","+","+","13795","+","1839","15755","2627","-2090","C","C","29743","24319","D","-22624","20374","+","D","2631","+","7296","-5109","9454","-10466","D","C","+","17555","+","12144","D","16710","27969","22863","D","8521","+","D","C","-25486","-1137","9782","25633","-12031","-11248","+","C","-13559","D","D"]
    

    It produces the correct expected output 1143440 when I run it locally; but an incorrect output of 20245 when I submit it. Could you kindly let me know the reason?

    I think it is due to some overflow, but since the return type of the function is an int I don't think that is an issue.


  • 0
    Z

    @BatCoder If the test case has a sequence of continuous "C", your code may not work.
    such as, [1,2,3,4,5,6,C,C,C,C,C,D]


  • 0

    @zestypanda Yep, got your point. Thank you! :)


  • 0
    W

    My approach.

    public int calPoints(String[] ops) {
        int ans = 0;
        Stack<Integer> stk = new Stack<>();
        for (String op : ops) {
            if (op.equals("C")) { ans -= stk.pop (); continue; }
            else if (op.equals ("D")) stk.push (stk.peek () * 2);
            else if (op.equals ("+")) stk.push (stk.get(stk.size () - 1) + stk.get(stk.size () - 2));
            else stk.push (Integer.valueOf (op));
            ans += stk.peek(); 
        }
        return ans;
    }
    

Log in to reply
 

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