题解 | #字符串加解密# 暴力
字符串加解密
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) 文章被收录于专栏
少壮不努力,老大勤刷题