题解 | #字符串加解密# 暴力

字符串加解密

http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

第一次想法,暴力破解,时间复杂度算是刚刚卡线

#时间:O(n^2),对应输入字符串每个字符都经过index()一遍,也就是O(n),总体就是O(n^2)
#空间:O(len(l1)+len(l2)+len(d1)+len(res))
def en_de_code(exp1,exp2):
    l1="abcdefghijklmnopqrstuvwxyz"
    l2="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    d1="0123456789"
    res=""
    if exp2 ==1:
        for i in exp1:
            if i.islower():
                index=(l1.index(i)+1)%26
                res+=l2[index]
            elif i.isupper():
                index=(l2.index(i)+1)%26
                res+=l1[index]
            elif i.isdigit():
                index=(d1.index(i)+1)%10
                res+=d1[index]
    elif exp2 ==-1:
        for i in exp1:
            if i.islower():
                index=(l1.index(i)-1)%26
                res+=l2[index]
            elif i.isupper():
                index=(l2.index(i)-1)%26
                res+=l1[index]
            elif i.isdigit():
                index=(d1.index(i)-1)%10
                res+=d1[index]
    print(res)

while True:
    try:
        en_de_code(input(),1)
        en_de_code(input(),-1)
    except:
        break

华为机试(python3) 文章被收录于专栏

少壮不努力,老大勤刷题

全部评论

相关推荐

专业嗎喽:个人信息名字太大,合到电话邮箱那一栏就行,有党员写过党,剩下其他全删,站空太大了 把实习经历丰富,放最前面,然后是个人评价,技能之类的,然后是学校信息。项目经历最后面,可以就选一个自己擅长的。 现在是学校不是92就扣分的,没必要放前面。 然后现在看重实习经历>竞赛经历(校园经历)>课程项目经历
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务