40ms python solution


  • 1
    Y
    class Solution(object):
        def countAndSay(self, n):
               \"""
            :type n: int
            :rtype: str
            """
            if n == 1:
                return "1";
            s = self.countAndSay(n-1);
            length = len(s);
            result = "";
            pre = s[0];
            preNum = 0;
            for i in range(length):
                tmp = s[i];
                if tmp != pre:
                    result = result + str(preNum) + pre;
                    pre = tmp;
                    preNum = 1;
                else:
                    preNum = preNum + 1;
            result = result + str(preNum) + pre;
            return result;

Log in to reply
 

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