C++ 0ms solution


  • 0
    H
    class Solution {
    public:
     string countAndSay(int n) {
        string res = "1", tmp;
        if(n==1) return res;
        int count, i, j;
        for(i = 2; i<=n; i++){
            tmp = res;
            res.clear(); count = 1;
            for(j = 1; j<tmp.size(); j++){
                if(tmp[j-1]==tmp[j]) count++;
                else{
                	res.push_back('0'+count);
                    res.push_back(tmp[j-1]);
                    count = 1;
                }
            }
            res.push_back('0'+count);
            res.push_back(tmp[j-1]);
        }
        return res;
    }
    };

Log in to reply
 

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