首页 > 其他 > 详细

leetcode Count and Say

时间:2015-05-13 16:18:01      阅读:98      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!