程序代码:
def hamming(n):
s = 1
import collections
q2 = collections.deque()
q3 = collections.deque()
q5 = collections.deque()
for x in range(n - 1):
q2.append(s * 2)
q3.append(s * 3)
q5.append(s * 5)
s = min(q2[0], q3[0], q5[0])
if q2[0] == s: q2.popleft()
if q3[0] == s: q3.popleft()
if q5[0] == s: q5.popleft()
return s
程序代码:
def hamming(n):
bases = [2, 3, 5]
expos = [0, 0, 0]
hamms = [1]
for _ in range(1, n):
next_hamms = [bases[i] * hamms[expos[i]] for i in range(3)]
next_hamm = min(next_hamms)
hamms.append(next_hamm)
for i in range(3):
expos[i] += int(next_hamms[i] == next_hamm)
return hamms[-1]