Count and Say

class Solution {
public:
    string countAndSay(int n) {
         string res;
         if(n<=0) return res;
         res += '1';
         for(int i=0;i<n-1;i++)
            res = s_2_s(res);
         return res;
    }
    string s_2_s(string pre){
        string aft;
        int len=0;
        if(pre=="") return pre;
        int i=0;
        char temp;
        while(pre[i]){
            len=1;
            temp=pre[i];
            i++;
            while(pre[i] && pre[i]==temp){
                i++;
                len++;
            }
            aft+=len+'0';  //这里没有问题,不会出现两位长度的
            aft+=temp;
        }
        cout<<aft;
        return aft;
    }
};

 

posted @ 2015-08-02 21:05  *桔子*  阅读(136)  评论(0编辑  收藏  举报