38. Count and Say

本文介绍了一个名为“计数与描述”的算法实现过程。该算法通过迭代方式生成一系列字符串,其中每个字符串都是对其前一个字符串中连续数字的计数和描述。例如,从'1'开始,后续字符串依次为'11'(一个1)、'21'(两个1)等。文章提供了Python代码示例,展示了如何使用循环和条件判断来完成这一任务。
class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        s=[]
        for i in range(n):
            if i==0:
                s.append("1")
                #print 1
                continue
            test=s[i-1]
            flag=0
            count=0
            j=0
            tmp=''
            while j<len(test):
                if(test[j]==test[flag]):
                    count+=1
                    j+=1
                    continue
                tmp=tmp+str(count)+test[flag]
                #s.append(tmp)
                #print tmp
                count=0
                flag=j
            if count>0:
                s.append(tmp+str(count)+test[flag])
            #print s
        return s[n-1]

  

转载于:https://www.cnblogs.com/xlqtlhx/p/8056170.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值