leetcode 38 外观数列

数列的每一项是对数列前一项的描述,比如11,对其描述就是21,2个1。递归完事

class Solution {
public:
    string countAndSay(int n) 
    {
        string result = "";
        if(n == 1)
        {
            result = "1";
            return result;
        }
        string temp = countAndSay(n-1);
        int i = 0;
        while(i<temp.length())
        {
            int j = 1;
            while(i+j<temp.length() && temp[i] == temp[i+j] )
            {
                j++;
            }
            result.insert(result.end(),j+'0');
            result.insert(result.end(),temp[i]); 
            i = i+j;           
        }
        return result;
    }
};

 

posted @ 2021-03-13 17:07  zhaohhhh  阅读(49)  评论(0)    收藏  举报