【每日一题1118】外观数列

:woman_mage: 给定一个正整数 n ,输出外观数列的第 n 项。「外观数列」是指一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。

示例:
第1项:1
第2项:11(第1项读成1个1)
第3项:21(第2项读成2个1)
第4项:1211(第3项读成1个2,1个1)

题目难度:初级
题目来源:外观数列

def solution(n: int) -> str:
    # your code

assert solution(1) == "1"
assert solution(4) == "1211"
assert solution(5) == "111221"

看不懂题目咋整 :joy:

去力扣看了一个小时答案,答案都没看懂

def solution(n: int) -> str:
    i = 1
    solution_num = '1'
    while True:
        if i == n:
            break
        new_solution_num = ''
        n_count = 1
        pre_n = ''
        for s in str(solution_num) + 'a':
            if pre_n == '':
                pre_n = s
            elif s == pre_n:
                n_count += 1
            else:
                new_solution_num = new_solution_num + str(n_count) + pre_n
                pre_n = s
                n_count = 1
        solution_num = new_solution_num
        i += 1
    return solution_num


assert solution(1) == "1"
assert solution(4) == "1211"
assert solution(5) == "111221"
2 个赞

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_1118(n:int)->int:
‘’’
外观数列:
外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。
1. 1
2. 11
3. 21
4. 1211
5. 111221
第一项是数字 1
描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 “11”
描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 “21”
描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 “1211”
描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 “111221”

链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnpvdm/

:param n:
:return:
'''
result = []
if n==1:
    return 1
else:
    num = solution_1118(n-1)
    return list_to_int(read_data(num))
pass

def test_solution():
# print(int_to_list(1))
# print(int_to_list(213))
# print(list_to_int(read_data(1)))
# print(list_to_int(read_data(11)))
# print(list_to_int(read_data(21)))
# print(list_to_int([1]))
# print(list_to_int([1,2, 3]))
assert solution_1118(1) == 1
assert solution_1118(2) == 11
assert solution_1118(3) == 21
assert solution_1118(4) == 1211
assert solution_1118(5) == 111221
pass

1 个赞

def solution(n: int) → str:

# 当前项数

i = 1

# 临时储存项描述的参数

temp = ""

# 连续次数统计

count = 0

# 项描述

l = "1"

# 判断是否大于1,大于1时进入循环

if n > 1:

    # 根据当前的项数,判断是否结束

    while i < n:

        # 取出当前项数的第一个值作为开始的判断条件

        temp_num = l[0]

        # 按当前项描述的长度循环

        for x in range(len(l)):

            # 从当前项描述的第一个开始对比

            if l[x] == temp_num:

                # 相等加1

                count += 1

                # 判断当前个数是否为最后一个

                if x == len(l) -1:

                    # 如果是最后一个,因为下面会跳出循环,所以需要将临时存储的项描述和次数统计以及当前个数的值按顺序拼接

                    temp = temp + str(count) + l[x]

            else:

                # 当前项数和前一项不等时,需先将之前统计个数以及值存储至临时的项描述

                temp = temp + str(count) + temp_num

                # 然后将当前的值作为判断条件

                temp_num = l[x]

                # 因为虽然当前值不等于之前的值,但是统计也应该为为1

                count = 1

                # 判断当前个数是否为最后一个

                if x == len(l) -1:

                    # 如果是最后一个,因为下面会跳出循环,所以需要将临时存储的项描述和次数统计以及当前个数的值按顺序拼接

                    temp = temp + str(count) + l[x]

        # 将临时存储的项描述赋值至项描述

        l = temp

        # 然后将临时存储的项描述清空,计算下次项描述时备用

        temp = ""

        # 然后统计次数清零

        count = 0

        # 项数加1

        i += 1

return l

assert solution(1) == “1”

assert solution(4) == “1211”

assert solution(5) == “111221”

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"