Could anyone help to check my code, why failed in OJ, but get the right result locally?


  • 0
    X

    Test Case: S.countAndSay(20) get the incorrect result on OJ server:
    111312211312111322212321121113121112131112132112311321322112111312211312112213211231132116121113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211
    But I can get the correct results locally.

    Here's the code:

    class Solution {
    public:
        string countAndSay(int n) {
            string res;
            string tmp;
    
            if (n == 0) return res;
    
            res = "1";
            while (n > 1) {
                int size = res.length();
                int idx = 0;
                tmp.clear();
                while (idx < size){
                    char c = res[idx];
                    size_t end = res.find_first_not_of(&c, idx);
                    if (end == string::npos){
                        end = size;
                    }
                    tmp.append(std::to_string(end - idx));
                    tmp.append(std::to_string(res[idx] - '0'));
                    idx = end;
                }
                res = tmp;
                n--;
            }
            cout <<res<<endl;
            return string(res);
            
        }
    

    };


  • 2
    S

    Do not cout anything when submit to judger.


  • 0
    X

    I have not submitted "cout ” , this code just for debug.


Log in to reply
 

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