-
Notifications
You must be signed in to change notification settings - Fork 0
/
038CountandSay.py
executable file
·38 lines (33 loc) · 1 KB
/
038CountandSay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n == 1:
return "1"
m_str = "1"
for j in range(1, n):
m_len = len(m_str)
start_pos = 0
m_newlist = []
while start_pos < m_len:
m_num = m_str[start_pos]
count = 1
# check the same number count
while start_pos < m_len:
if start_pos + 1 < m_len:
if m_str[start_pos + 1] == m_str[start_pos]:
start_pos += 1
count += 1
else:
break
else:
break
m_value = str(count) + m_num
m_newlist.append(m_value)
start_pos += 1
m_str = "".join(m_newlist)
return m_str
a = Solution()
print a.countAndSay(4)