Count and Say

直接模拟即可,就是int转string麻烦一点

    string countAndSay(int n) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        //just do it straight;
        string res = "1";
        if(n<=1)
            return res;
        for(int i=2;i<=n;i++)
        {
            string prev(res);
            res = "";
            int cnt = 1;
            int j;
            for(j=1;j<prev.size();j++)
            {
                if(prev[j]==prev[j-1])
                    cnt++;
                else
                {
                    stringstream ss;
                    string s;
                    ss<<cnt;
                    ss>>s;
                    res+=s;
                    res+=prev.substr(j-1,1);
                    cnt = 1;
                }
            }
            
            if(cnt>0)
            {
                stringstream ss;
                string s;
                ss<<cnt;
                ss>>s;
                res+=s;
                res+=prev.substr(j-1,1);
            }
            
        }
        
        return res;
            
        
    }

  

posted @ 2013-10-09 17:01  summer_zhou  阅读(146)  评论(0编辑  收藏  举报