Count and Say

来源:互联网 发布:学生赚软件下载 编辑:程序博客网 时间:2024/06/06 19:37
class Solution {public:    string countAndSay(int n)     {        if(n<=0)            return "";        string result="1";        for(int i=2;i<=n;++i)        {            string temp="";            int start=0;            int end=0;            for(int j=1;j<result.size();++j)            {                if(result[j]==result[j-1])                {                    end=j;                }                   else                {                    temp+=to_string(end-start+1)+result[j-1];                    start=j;                    end=j;                }                                }            temp+=to_string(end-start+1)+result[end];            result=temp;        }        return result;            }};

0 0
原创粉丝点击