A series of numbers is a sequence of sequential sequences in which an integer is taken, and the next number is counted as a row. The first five items are as follows:
1. . 113. 214. 12115. 111221
1
Be read as "one 1"
( "一个一"
), ie 11
.
11
Be read as "two 1s"
( "两个一"
), ie 21
.
21
Be read as "one 2"
, " one 1"
( "一个二"
, "一个一"
), ie 1211
.
Given a positive integer n(1≤ n ≤30), outputs the nth item of the datagram sequence.
Note: The integer order is represented as a string.
Example 1:
Input: 1 output: "1"
Example 2:
Input: 4 output: "1211"
Public StaticString Countandsay (intN) {String initialstring= "1";//first time starting with "1" for(intI=1; i<n; i++) { //define the results of StringBuffer receiving a datagramStringBuffer StringBuffer =NewStringBuffer (); String[] Strings= Initialstring.split (""); //define count to receive the same number of characters intCount = 1; /*** Count the same characters, if same, count self-increment * If different from the latter one, add the number to the StringBuffer (equivalent to "several") * and then Cou NT re-assignment to 1, continuation of the next character Count*/ for(intj=0; j<strings.length; J + +) { if(j = = Strings.length-1 | |!strings[j].equals (strings[j+1]) {stringbuffer.append (Count+Strings[j]); Count= 1; Continue; } if(Strings[j].equals (strings[j+1]) {count++; }} initialstring=stringbuffer.tostring (); } returninitialstring; }
Leetcode 38. Count