leetcode:Count and Say【Python版】

简介:

一次AC

字符串就是:count+char

复制代码
 1 class Solution:
 2     # @return a string
 3     def countAndSay(self, n):
 4         str = "1"
 5         for i in range(n-1):
 6             tmp = str
 7             str = ""
 8             c = tmp[0]
 9             cnt = 1
10             for j in range(1,len(tmp)):
11                 if tmp[j] == tmp[j-1]:
12                     cnt += 1
13                 else:
14                     str += ("%d"%cnt + tmp[j-1])
15                     cnt = 1
16             str += ("%d"%cnt + tmp[len(tmp)-1])
17         return str
复制代码

 

本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4033890.html,如需转载请自行联系原作者

相关文章
|
20天前
|
API Python
[AIGC] 使用Python刷LeetCode:常用API及技巧指南
[AIGC] 使用Python刷LeetCode:常用API及技巧指南
|
20天前
|
算法 索引 Python
leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)
leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)
41 0
|
20天前
|
存储 索引 Python
leetcode-350:两个数组的交集 II(python中Counter的用法,海象运算符:=)
leetcode-350:两个数组的交集 II(python中Counter的用法,海象运算符:=)
33 0
|
20天前
|
开发者 索引 Python
【python刷题】LeetCode 2057E 值相等的最小索引(5种简单高效的解法)
【python刷题】LeetCode 2057E 值相等的最小索引(5种简单高效的解法)
31 0
|
12月前
|
Python
Python|leetcode-访问所有点的最小时间
Python|leetcode-访问所有点的最小时间
53 0
Python|Leetcode《539》|最小时间差
Python|Leetcode《539》|最小时间差
Python|Leetcode《1220》|统计元音字母序列的数目
Python|Leetcode《1220》|统计元音字母序列的数目
Python|Leetcode《334》|递增的三元子序列
Python|Leetcode《334》|递增的三元子序列
|
算法 Python
Python|Leetcode《306》|累加数
Python|Leetcode《306》|累加数
|
存储 Unix Python
Python|Leetcode《71》|简化路径
Python|Leetcode《71》|简化路径