python exercise #1

755 ワード

class Solution:
    # @return a string
    def countAndSay(self, n):
        n=int(n)
        if n==1:
            return "1"
        self.data=[1]
        for i in range(1,n):
            ans=[]
            pre=self.data[0]
            count=0
            for data in self.data:
                if data==pre:
                    count=count+1
                else:
                    ans.append(count)
                    ans.append(pre)
                    pre=data
                    count=1
            ans.append(count)
            ans.append(pre)
            self.data=ans
        answer_string=""
        for data in self.data:
            answer_string=answer_string+str(data)
        return answer_string