这道题用递归做def missing_digitsn Given a number a that is in sorted increasing order return the number of missing digits in n A missing digit is a number between the first and last digit of a that
def missing_digits(n): if n < 10: return 0 last_digit = n % 10 second_last_digit = (n // 10) % 10 if last_digit - second_last_digit == 1: return missing_digits(n // 10) else: return (last_digit - second_last_digit - 1) + missing_digits(n // 10)
testing the function
print(missing_digits(1248)) # 4 print(missing_digits(1122)) # 0 print(missing_digits(123456)) # 0 print(missing_digits(3558)) # 3 print(missing_digits(35578)) # 2 print(missing_digits(12456)) # 1 print(missing_digits(16789)) # 4 print(missing_digits(19)) # 7 print(missing_digits(4)) #
原文地址: http://www.cveoy.top/t/topic/iTyO 著作权归作者所有。请勿转载和采集!