1957

无聊蛋疼的1957写的低端博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[leetcode]Count and Say

Posted on 2014-01-10 00:17  1957  阅读(134)  评论(0编辑  收藏  举报

按照说明模拟。。。

stringstream挺好用的

class Solution {
public:
    string count(const string& now) {
        stringstream ss;
        int i = 0;
        int size = now.size();
        int prev = -1;
        while(i < size) {
           while(i + 1< size && now[i] == now[i + 1]) i++;
           ss << (i - prev) << now[i];
           prev = i;
           i++;
        }
        return ss.str();
    }
    string countAndSay(int n) {
        string now = "1";
        for(int i = 1 ; i < n  ; i++) {
            now = count(now);
        }
        return now;
    }
};