Straightforward python solution


  • 0
    Y

    class Solution(object):
    def countAndSay(self, n):
    """
    :type n: int
    :rtype: str
    """
    s = '1'
    for _ in range(n-1):
    # count to remember current number frequency, temp to record the current step
    count, current, temp = 0, s[0], ''
    for ss in s:
    if ss == current:
    count += 1
    else:
    temp += (str(count) + current)
    current = ss
    count = 1
    temp += (str(count) + current)
    s = temp
    return s


  • 0
    Y

    @yuyuyu0905 said in Straightforward python solution:

    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        s = '1'
        for _ in range(n-1):
            # count to remember current number frequency, temp to record the current step
            count, current, temp = 0, s[0], ''
            for ss in s:
                if ss == current:
                    count += 1
                else:
                    temp += (str(count) + current)
                    current = ss
                    count = 1
            temp += (str(count) + current)
            s = temp
        return s

Log in to reply
 

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