count and say leetcode @python

https://leetcode.com/submissions/detail/44515521/

 

class Solution(object):
    
    def count(self,s):
        res = ""
        count = 0;
        cur = "#"
        for i in s:
            if i!=cur:
                if cur!='#':
                    res+=str(count)+cur
                cur=i
                count=1
            else:
                count+=1
        res+=str(count)+cur
        return res
        
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        s='1'
        for i in range(2,n+1):
            s = self.count(s)
        return s
        

 

posted on 2015-10-30 15:00  星空守望者--jkmiao  阅读(100)  评论(0)    收藏  举报