码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Count and Say

时间:2015-03-19 07:47:22      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

Pretty straight forward. count numbers and put char.

 1 class Solution {
 2 public:
 3     string getNext(string s) {
 4         ostringstream oss;
 5         char rec = s[0];
 6         int len = s.size(), num = 1;
 7         for (int i = 1; i < len; i++) {
 8             if (rec != s[i]) {
 9                 oss << char(num + 0) << rec;
10                 rec = s[i];
11                 num = 1;
12             } else num++;
13         }
14         oss << char(num + 0) << rec;
15         return oss.str();
16     }
17     string countAndSay(int n) {
18         string result = "1";
19         for (int i = 2; i <= n; i++) {
20             result = getNext(result);
21         }
22         return result;
23     }
24 };

 

LeetCode – Refresh – Count and Say

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4349360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!