38.Count and Say

【题目】

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221

1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.

Given an integern, generate thenthterm of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input:
 1

Output:
 "1"

Example 2:

Input:
 4

Output:
 "1211"

【思路】

是的,這題難就是難在題目,老實說我一開始真的看不懂,後來找到別人的中文題意後,就可以比較快理解,頭痛。

n = 1的時輸出字符串1;

n = 2的時,數上次字符串中的數值個數,因為上次字符串有1個1,所以輸出11;

n =3時,由於上次字符是如圖11所示,有2個1,所以輸出21;

n =4時,由於上次字符串是21,有1個2和1個1,所以輸出1211依次類推,寫個countAndSay(n)的函數返回字符串。

Reference:https://blog.csdn.net/xygy8860/article/details/46821417

後續處理方式應該是用遞迴的做法處理

【解法】

☆JAVA

JAVA應

p

☆Python

跟J

def

results matching ""

    No results matching ""