# Pre-populate a look-up array with bit-reversed integers from 0 to 255
bytearray = []
for i in range(0, 256):
bytearray.append(int('{:08b}'.format(i)[::-1], 2))
# Reverses the bits of each character in the input string and returns the result
# as a string
def revstr(string):
return ''.join([chr(bytearray[ord(a)]) for a in list(string)])
print "JK".encode("hex") # 0x4a4b
print revstr("JK").encode("hex") # 0x52d2
接受了建议后,我的解决方案如下:
如果您的目标是速度,并且您使用的是ASCII,因此您只有256个8位值要处理,请事先计算反向字节值并将它们放入
bytearray
,然后通过索引到bytearray
中来查找它们。你知道吗如果要对大量字符执行此操作,那么只有256个ascii字符。将反转的字符串存储在hashmap中,并在hashmap上进行查找。这些查找的时间复杂度是O(1),但是有一个固定的设置时间。你知道吗
相关问题 更多 >
编程相关推荐