求长度为n的对称数

 

 

# 对称字符串,101, 96, 88. 中间字符: "", 0, 1, 8  两端字符: 1, 8, 6, 9
# 知识库: mapping = {"1":1, "8":8, "6":9, "9":6}
s = "1110111"

def solve(s):
    # 双指针,left:n->1, right: left+1:n
    n = len(s)
    for i in range(n-1, -1, -1):
        for j in range(i+1, n):
            print(i, j)
            
def centroSymmetric(n):
    # category discussion
    if not n:
        return
    if n == 1:
        return ['0', '1', '8']
    if n == 2:
        return ['11', '69', '88', '96']
    # save the list result
    dp = [[]] * (n + 1) 
    dp[1] = ['0', '1', '8']
    dp[2] = ['11', '69', '88', '96']
    
    for i in range(3, n+1):
        pre_dp = i - 2
        temp = []
        for j in dp[pre_dp]:
            if i != n:
                # 最后一个数的0没有意义,前面要使用,因此要加入进来
                temp.append('0' + j + '0')
            temp.append('1' + j + '1')
            temp.append('6' + j + '9')
            temp.append('8' + j + '8')
            temp.append('9' + j + '6')
        dp[i] = temp
        
    return dp[n]
centroSymmetric(4)

 

posted @ 2022-12-15 11:09  今夜无风  阅读(23)  评论(0编辑  收藏  举报