forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
38_Count and Say.py
64 lines (60 loc) · 1.96 KB
/
38_Count and Say.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 29 12:49:25 2019
@author: leiya
"""
#0709 sliding window
'''
这道题的难点在于如果start,end从某个位置开始,可能会一直一样,这样在循环里可能没办法把这部分统计进去,
可以根据start,end最后是否重合来把这部分特殊情况解决掉
注意每次改变滑动窗口以后需要移动窗口指针
'''
class Solution:
def countAndSay(self, n: int) -> str:
res = '1'
for i in range(2,n+1):
#每次要用新的new_res,start
new_res = ''
start = 0
for end in range(len(res)):
if res[end] != res[start]:
new_res += str(end-start) + res[start]
start = end
if end != start:
new_res += str(end-start+1) + res[start]
else:
new_res += '1' + res[end]
res = new_res
return res
#updated 0629 这道题只能通过j与j+1比较,有题目本身的特性决定,每次通过小循环找到重复数字的个数
class Solution:
def countAndSay(self, n: int) -> str:
temp = '1'
for i in range(n-1):
j = 0
newtemp = ''
while j < len(temp):
count = 1
while j < len(temp) - 1 and temp[j] == temp[j+1]:
count += 1
j += 1
newtemp += str(count) + temp[j]
j += 1
temp = newtemp
return temp
class Solution:
def countAndSay(self, n: int) -> str:
seq = '1'
for i in range(n-1):
seq = self.getNext(seq)
return seq
def getNext(self,seq):
i,next_seq = 0
while i < len(seq):
count = 1
while i < len(seq) - 1 and seq[i] == seq[i+1]:
count += 1
i += 1
next_seq += str(count) + seq[i]
i += 1
return nex_seq