class Solution {
public:
    //when see str, return count and say of str
    string process(const string& str){
        if (str == "")
            return "";

        int l = 0;
        int r = 0;
        int count = 0;
        string result = "";
        for (; r < str.length(); ){
            if (str[r] == str[l]){
                count++;
                r++;
            }
            else{//每当发现str[r]和str[l]不一样时,将l到r-1的append到result
                char tmp[5];
                memset(tmp, 0, 5);
                sprintf(tmp, "%d%d", count, str[l] - '0');
                result.append(tmp);
                count = 0;
                l = r;
            }
        }
        char tmp[5];
        memset(tmp, 0, 5);
        sprintf(tmp, "%d%c", count, str[l]);
        result.append(tmp);

        return result;
    }

    string countAndSay(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (n < 1)
            return "";

        string str = "1";
        if (n == 1)
            return str;
        
        for (int i = 1; i < n; i++){
            string tmp = process(str);
            str.assign(tmp);
        }

        return str;
    }
};

 

 

 

 

 

 

EOF

相关文章:

  • 2021-08-15
  • 2021-08-11
  • 2021-09-07
  • 2021-10-10
  • 2022-01-04
  • 2021-11-05
猜你喜欢
  • 2022-02-14
  • 2021-07-29
  • 2022-02-05
  • 2021-07-07
  • 2021-10-02
  • 2021-09-10
  • 2021-11-14
相关资源
相似解决方案