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

*Count and Say

时间:2015-10-09 13:55:02      阅读:130      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!