def int_to_list(n : int)->list:
‘’‘整数转换为list’’’
result_list =
while n > 0:
result_list.insert(0, n%10)
n = int(n/10)
return result_list
def list_to_int(list_data:list)->int:
result = 0
for n in list_data:
result = result*10 + n
return result
def read_data(n:int)->list:
‘’’
给定数字n,按照外观数列的方式读取出来
:param n:
:return:
‘’’
result =
list_data = int_to_list(n)
for i in range(0, len(list_data)):
for j in range(i + 1, len(list_data)):
if list_data[i] != list_data[j]:
result.append(j - i)
result.append(list_data[i])
i = j
break
else:
result.append(len(list_data) - i)
result.append(list_data[i])
break
return result
def solution(n: int) -> str:
count=2
result="11"
if n==1:
return "1"
if n==2:
return "11"
else:
while count<n:
count+=1
list1 = []
a=""
for i in result+'+':
if a=='':
a+=i
elif i not in a:
list1.append(str(len(a))+a[0])
a+=i
a=a[-1]
else:
a+=i
result=''.join(list1)
return result
assert solution(1) == "1"
assert solution(4) == "1211"
assert solution(5) == "111221"
def solution(n: int) -> str:
if n == 1: return str(n)
be_read = solution(n-1)
num = 0
last_w = ""
result = ""
for w in be_read:
if w != last_w:
if last_w != "": result += str(num) + last_w
last_w = w
num=0
num += 1
result += str(num) + last_w
return result
def solution(n: int) -> str:
if n == 1:
return "1"
m = solution(n-1)
j = 0
l = ''
for i, value in enumerate(m):
if value != m[j]:
l += str(i-j)+m[j]
j = i
l += str(i+1-j)+m[j]
return l
def solution(n):
if n == 1:
return '1'
data = solution(int(n)-1)
result = count_num(data)
return result
def count_num(data):
result = ''
count = 1
if len(data) == 1:
result = result + '1' + data[0]
for i in range(0, len(data)-1):
if data[i] == data[i+1]:
if i == len(data)-2:
result = result + str(count+1) + data[i]
else:
count = count + 1
continue
else:
result = result + str(count) + data[i]
if i == len(data)-2:
result = result + "1" + data[i+1]
count = 1
# print(result)
return result
assert solution(1) == "1"
assert solution(4) == "1211"
assert solution(5) == "111221"
assert solution(20) == "11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211"