首页 > 其他 > 详细

Leetcode#38 Count and Say

时间:2015-02-02 09:26:02      阅读:318      评论:0      收藏:0      [点我收藏+]

原题地址

 

简单模拟题

从1开始推即可。不知道有没有规律可以直接求出n

 

代码:

 1 string countAndSay(int n) {
 2         if (n <= 0)
 3             return "";
 4             
 5         string str = "1";
 6         while (--n) {
 7             string next;
 8             int count = 1;
 9             char last = str[0];
10             for (int i = 1; i < str.length(); i++) {
11                 if (str[i] == last)
12                     count++;
13                 else {
14                     next += count + 0;
15                     next += last;
16                     last = str[i];
17                     count = 1;
18                 }
19             }
20             next += count + 0;
21             next += last;
22             str = next;
23         }
24         
25         return str;
26 }

 

Leetcode#38 Count and Say

原文:http://www.cnblogs.com/boring09/p/4266800.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!