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

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 where 1 ≤ n ≤ 30, generate the nth term 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"

Python

 
class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        1.     1
        2.     11
        3.     21
        4.     1211
        5.     111221
        6.     312211
        7.     13112221

        """
        d = {1: '1', 2: '11', 3: '21', 4: '1211', 5: '111221', 6: '312211', 7: '13112221', 8: '1113213211', 9: '31131211131221',
             10: '13211311123113112211', 11: '11131221133112132113212221', 12: '3113112221232112111312211312113211',
             13: '1321132132111213122112311311222113111221131221',
             14: '11131221131211131231121113112221121321132132211331222113112211',
             15: '311311222113111231131112132112311321322112111312211312111322212311322113212221',
             16: '132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211',
             17: '11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221',
             18: '31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211',
             19: '1321132132211331121321231231121113112221121321132122311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112111331121113122112132113213211121332212311322113212221',
             20: '11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211'}

        if n in d:
            print 1111
            return d[n]
        result = d[20]
        for i in range(19, n-1):
            result = self.get_new_result(result)
        return result

    def get_new_result(self, last_resule):
        result = ""
        num = 0
        temp_result = "a"
        for i in last_resule:
            if i == temp_result:
                num += 1
            else:
                if temp_result == "a":
                    temp_result = i
                    num = 1
                else:
                    result += str(num) + temp_result
                    temp_result = i
                    num = 1
        result += str(num) + temp_result
        return result

发表评论