首页 > 其他 > 详细

*Count and Say

时间:2015-10-09 13:55:01      阅读:274      评论:0      收藏:0      [点我收藏+]

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

 

 

 1 public class Solution {
 2     public String countAndSay(int n) 
 3     {
 4         String oldString = "1";
 5         while(--n>0)
 6         {
 7             StringBuilder sb = new StringBuilder();
 8             char[] oldChar = oldString.toCharArray();
 9             for(int i=0;i<oldChar.length;i++)
10             {
11                 int count =1;
12                 while((i+1)<oldChar.length&&oldChar[i]==oldChar[i+1])
13                 {
14                     count++;
15                     i++;
16                 }
17                 sb.append(String.valueOf(count)+String.valueOf(oldChar[i]));
18                 
19             }
20             oldString = sb.toString();
21             
22         }
23         
24         return oldString;
25     }
26 }

 

*Count and Say

原文:http://www.cnblogs.com/hygeia/p/4863685.html

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