给一根绳子,你应该还它的美丽。字符串s的美是由字符串s中的string.The索引中的每个索引的最大美定义的,字符串s中的索引的美由公式(左侧^2* righti^2)表示,其中:
si.
是指数j的个数,ij
功能美应该回归s的美。
示例:
beauty("abc") == 1
beauty("aabb") == 0到目前为止我所拥有的是:
我对左右两种索引具体指的是什么以及这些例子是如何得到它们的值感到困惑。
def beauty(phrase):
splitphrase = phrase.split()
asciilist = []
for x in splitphrase:
asciilist.append(ord(splitphrase[x])^2)发布于 2021-12-05 02:44:00
您可以在理解中使用枚举计算左和右的值,然后将其压缩到一起,得到每个左/右对的乘积的最大值:
def beauty(s):
left = ( sum(p<c for p in s[:i])**2 for i,c in enumerate(s) )
right = ( sum(p>c for p in s[i:])**2 for i,c in enumerate(s,1) )
return max(l*r for l,r in zip(left,right))
print(beauty("abc")) # 1
print(beauty("aabb")) # 0
print(beauty("zabcd")) # 4请注意,给定预期的结果,我将left[i]^2和right[i]^2解释为值的平方(与二进制XOR操作相反)
https://stackoverflow.com/questions/70230916
复制相似问题