Leetcode 38. Count

Source: Internet
Author: User

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

1Be read as "one 1" ( "一个一" ), ie 11 .
11Be read as "two 1s" ( "两个一" ), ie 21 .
21Be 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.