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

leetcode Count and Say

时间:2015-05-13 16:20:42      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:

代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 
 5 using namespace std;
 6 
 7 
 8 string countAndSay(int n) 
 9 {
10     string s = "1";
11     int i = 0; 
12     while (--n > 0)
13     {
14         char c = s[0];
15         string b = "";
16         i = 0;
17         while (i < s.length())
18         {
19             i++;
20             int count = 1;
21             while (i<s.length()&&s[i] == c)
22             {
23                 count++;
24                 i++;
25             }
26             b = b + (char)(count + 0);
27             b = b + c;
28             if (i == s.length())
29             {
30                 break;
31             }
32             c = s[i];
33         }
34         s = b;
35     }
36     return s;
37 }
38 
39 int main()
40 {
41     cout << countAndSay(2) << endl;
42 }

 

leetcode Count and Say

标签:

原文地址:http://www.cnblogs.com/chaiwentao/p/4500589.html

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