leetcode_Count and Say
2017-05-18 20:33
429 查看
Problem description:
The count-and-say sequence is the sequence of integers beginning as follows:
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
My solution(python):
class Solution(object):
def countAndSay(self, n):
i = 4
s = str(1)
slist = ['1', '11', '21']
for i in range(i, n+1):
scount = 0 # 记录i+1中字符串的每个位数
count = 0 # 记录字符串中每组相同数字的个数
s0 = slist[i-2] # s0代表之前的字符串
s = '' # s 代表生成新的kong字符串
k = 0
for k in range(k, len(s0)):
if k == len(s0)-1:
count += 0
s = s + str(count + 1) + s0[k]
slist.append(s)
elif k < len(s0)-1 and s0[k] == s0[k+1]:
count += 1
else:
s = s + str(count + 1) + s0[k]
count = 0
return slist[n-1]
"""
:type n: int
:rtype: str
"""
I used two layers of circulation.
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as
"one 1"or
11.
11is read off as
"two 1s"or
21.
21is read off as
"one 2, then
one 1"or
1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
My solution(python):
class Solution(object):
def countAndSay(self, n):
i = 4
s = str(1)
slist = ['1', '11', '21']
for i in range(i, n+1):
scount = 0 # 记录i+1中字符串的每个位数
count = 0 # 记录字符串中每组相同数字的个数
s0 = slist[i-2] # s0代表之前的字符串
s = '' # s 代表生成新的kong字符串
k = 0
for k in range(k, len(s0)):
if k == len(s0)-1:
count += 0
s = s + str(count + 1) + s0[k]
slist.append(s)
elif k < len(s0)-1 and s0[k] == s0[k+1]:
count += 1
else:
s = s + str(count + 1) + s0[k]
count = 0
return slist[n-1]
"""
:type n: int
:rtype: str
"""
I used two layers of circulation.
相关文章推荐
- leetcode ——Count and Say
- [Leetcode]Count and Say
- [LeetCode] Count and Say
- LeetCode刷题笔录Count and Say
- Leetcode:Count and Say
- [leetcode 38] Count and Say
- Leetcode: Count and Say
- Leetcode 38 Count and Say
- [LeetCode] Count and Say
- leetcode -- Count and Say -- 理解题意
- 开始刷leetcode day30:Count and Say
- LeetCode - 38 - Count and Say
- LeetCode 38: Count and Say
- Leetcode--Count and Say
- [Leetcode]#38 Count and Say
- [leetcode]Count and Say
- Leetcode NO.38 Count and Say
- leetcode_Count and Say
- LeetCode: Count and Say
- leetcode记录——Count and Say