标签:
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 }
标签:
原文地址:http://www.cnblogs.com/qq1029579233/p/4480289.html