Count and Say

来源:互联网 发布:crt结束tomcat端口 编辑:程序博客网 时间:2024/06/06 00:34
class Solution {  
public:  
    string revolution(string s)  
    {  
        string ret="";
        int count=1;
       char pre=s[0];
       for(int i=0;i<s.size();i++)
       {
           if(s[i+1]==pre)
           count++;
           else
           {
               char tmp=count+'0';
               ret=ret+tmp+pre;
               pre=s[i+1];
               
               count=1;
           }
       }
       return ret;
    }  
    string countAndSay(int n) {  
     string ret="1";
     int j=1;
     while(j<n){
          ret=revolution(ret);
          j++;
     }
    
     return ret;
    }  
};  
0 0
原创粉丝点击