Title Description:
The Count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as "one 1" or 11 .
11is read off as "two 1s" or 21 .
21is read off "one 2 as, then one 1" or 1211 .
Given an integer n, generate the nth sequence.
Note:the sequence of integers would be represented as a string.
Problem Solving Ideas:
From the first start count, for input n, the number of n-1 times can be.
The code is as follows:
public class Solution {public String countandsay (int n) {int i = 1;if (n <= 0) return null; String s = "1", while (I <= n-1) {s = Countandsay (s); i++;} return s;} Public String Countandsay (string s) {int count = 1; StringBuffer sb = new StringBuffer (); for (int i = 1; i < s.length (); i++) {if (S.charat (i-1) = = S.charat (i)) count++;e LSE {Sb.append (count). Append (S.charat (i-1)); count = 1;}} Sb.append (count). Append (S.charat (S.length ()-1)); return sb.tostring ();}}
Java [Leetcode 38]count and Say