Skip to content

Commit 638a4fc

Browse files
authored
Create Solution.py to 0038 with python2 -24ms
1 parent d8dddb7 commit 638a4fc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def countAndSay(self, n):
3+
s = '1' #初始化第一个数字
4+
for i in range(n-1): #n-1个数
5+
temp = '' #一个空数列
6+
num = s[0] #s的第一位数字
7+
count = 0 #当前数字的个数
8+
9+
for j in s: #遍历s中的每个字母
10+
if num == j: #如果这个字母和num一样
11+
count += 1 #计数+1
12+
else:
13+
temp += str(count)+str(num) #存下“几”个“什么数”
14+
num = j #num改成当前数字
15+
count = 1 #计数回到1
16+
temp += str(count)+str(num) #加的是最后一个
17+
s = temp
18+
return s

0 commit comments

Comments
 (0)