阿里笔试08.14算法岗



阿里0814,算法岗第二题  咋做呀,想着暴力蹭点分,一点也没蹭上呀,怀疑又没理解题意。。。o(╥﹏╥)o

def dp(s,dicts):
    # print(1111)
    global count
    cur = ''.join(s)
    # print(cur)
    if cur in dicts:
        return
    dicts.add(cur)
    print(cur)
    count = (count+1)%1000000007
    # print(cur)
    # print(count)
    index = 0
    while index<length-1:
        pre = int(s[index])
        last = int(s[index+1])
        if pre>0 and last<9:
            s[index] = str(pre-1)
            s[index+1] = str(last+1)
            # if ''.join(s) not in dicts:
            dp(s,dicts)
            s[index] = str(pre)
            s[index + 1] = str(last)
        if pre<9 and last>0:
            s[index] = str(pre+1)
            s[index+1] = str(last-1)
            # if ''.join(s) not in dicts:
            dp(s,dicts)
            s[index] = str(pre)
            s[index + 1] = str(last)
        index+=1

q = int(input())
for i in range(q):
    s = [each for each in input()]
    if len(s)<2:
        print(0)
        continue
    count = -1
    length = len(s)
    dicts = set()
    dp(s,dicts)
    print(count)


#笔试题目##阿里巴巴#
全部评论
第二题,二维动态规划
1 回复
分享
发布于 2020-08-14 21:19
蹲一个题解 是类似于这样的组合数学问题吗?
点赞 回复
分享
发布于 2020-08-14 20:17
博乐游戏
校招火热招聘中
官网直投
笔试时没想明白,后面重新想了一下: def similarCount(s):     N = len(s)     if N == 1:         return 0     dp = [[0] * 10 for _ in range(N)]     dp[0][int(s[0])] = 1     for i in range(1, N):         for j in range(10):             cur = int(s[i])             diff = j - cur             for k in range(10):                 pre = k + diff                 if 0 <= pre < 10:                     dp[i][j] += dp[i-1][pre]         print(dp[i])     ans = 0     for j in range(10):         ans += dp[-1][j]         ans %= 1000000007     return ans - 1 q = int(input()) for i in range(q):     s = input()     ans = similarCount(s)     print(ans) 不知道有没有bug
点赞 回复
分享
发布于 2020-08-15 00:19
直觉所有长度一样且数字和一样的都相似,然后就简单了
点赞 回复
分享
发布于 2020-08-15 11:25
第2题题目是什么?
点赞 回复
分享
发布于 2020-08-17 11:19

相关推荐

点赞 评论 收藏
转发
点赞 3 评论
分享
牛客网
牛客企业服务