[Leetcode] Count and Say

    string countAndSay(int n) {
        string res("1");
        while(--n){
            string cur;
            int len = res.size();
            for(int i = 0; i != len ;++i){
                int num = res[i] - '0';// num
                int cnt = 1;
                int j = i + 1;
                while(res[j] == (num + '0') && j < len){ //一定是(num + '0'),刚开始下写成了num,郁闷
                    ++cnt;
                    ++i;
                    ++j;
                }
                cur += (cnt + '0');
                cur += (num + '0');
            }
            res = cur;
        }
        return res;
    }

posted on 2015-10-11 19:39  泉山绿树  阅读(16)  评论(0)    收藏  举报

导航