LeetCode 1015
这个主要还是个数学问题,需要知道一些取模的特性
class Solution:
def smallestRepunitDivByK(self, K: int) -> int:
#a[n+1] = a[n]*10 + 1
#a[n+1]%k = ((a[n]%k)*10 +1)%k
if K == 1: return 1
if K % 2 == 0: return -1
if K % 5 == 0: return -1
remainders = set()
r = 1
count = 1
while r != 0:
if r in remainders:
return -1
remainders.add(r)
r = ((r % K) * 10 + 1)%K
count +=1
return count
sln = Solution()
assert(sln.smallestRepunitDivByK(1) == 1)
assert(sln.smallestRepunitDivByK(2) == -1)
assert(sln.smallestRepunitDivByK(3) == 3)
print("1015: All cases passed, great!")