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

Count and Say

时间:2015-05-05 23:21:15      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/count-and-say/

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.

Note: The sequence of integers will be represented as a string.

 1 public class Solution {
 2     public static String countAndSay(int n) {
 3         String ans="1";
 4         for(long i=1;i<n;i++){
 5             String t="";
 6             long len=ans.length();
 7             char x=ans.charAt(0);
 8             int cout=1;
 9             for(long j=1;j<len;j++){
10             char y=ans.charAt((int)j);
11             if(x==y){cout++;}
12             else{t=t+cout+x;x=y;cout=1;}
13             }
14             t=t+cout+x;
15             ans=t;
16         }
17         return ans;
18     }
19     public static void main(String[]args){
20     System.out.println(countAndSay(30));
21     }
22 }

 

Count and Say

标签:

原文地址:http://www.cnblogs.com/qq1029579233/p/4480289.html

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