Whether there is a solution which is not brute force???


  • -1
    H

    author = 'ZhongH'

    class Solution:
    # @param {integer} n
    # @return {string}
    def init(self):
    self.str = []

    def countAndSay(self, n):
        self.str.append("1")
        self.str.append("11")
        i = 2
        while i < n:
            j = 0
            index = 0
            res = 0
            tmp = ""
            l = len(self.str[i-1])
            while j < l:
                if self.str[i-1][j] == self.str[i-1][index]:
                    res += 1
                else:
                    tmp+=str(res)
                    tmp+=self.str[i-1][index]
                    res=1
                    index = j
                j+=1
            tmp+=str(res)
            tmp+=self.str[i-1][index]
            i+=1
            self.str.append(tmp)
        return  self.str[n-1]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.