python 3, easy to understand


  • 0
    M
    class Solution:
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            nums = ''
            arr = ''
            for i in range(n):
                times = 1
                for i, number in enumerate(nums):
                    if i == 0:
                        continue
                    elif number == nums[i - 1]:
                        times += 1
                    else:# i > 0 and nums[i - 1] != number
                        arr += str(times) + (nums[i - 1] if len(nums) else '')
                        times = (1 if i != len(nums) else 0)
                if times:
                    arr += str(times) + (nums[i] if len(nums) else '')
                arr, nums = '', arr
            return nums
    

Log in to reply
 

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