📜  最接近 N 的最小数仅由奇数位组成

📅  最后修改于: 2021-10-26 06:23:08             🧑  作者: Mango

给定一个整数N ,任务是找到最接近N 且只有奇数位的数字。如果存在多个这样的数字,则打印最小值。

例子:

朴素的方法:解决问题的最简单的方法是迭代并找到最接近的小于和大于 N 的数字,只有奇数作为其数字。请按照以下步骤解决问题:

  1. 如果N 的所有数字都是奇数,则打印N作为答案。
  2. 找到最接近的更小和更大的数字,并返回与N差异最小的数字。如果发现两者与N等距,则打印较小的数字。

时间复杂度:O(10 (len(N) – 1) )
辅助空间: O(1)

高效方法:按照以下步骤优化上述方法:

  1. 如果N 的所有数字都是奇数,则返回N
  2. 通过以下步骤有效地计算与N最接近的较小数字:
    • 从左到右求N中第一个偶数的位置,即pos
    • 如果第一个偶数为0 ,则将0替换为9并迭代所有前面的数字,对于每个数字,如果发现为1 ,则将其替换为9 。否则,将数字减 2 并返回数字。
    • 如果没有发现前面的数字超过1 ,则将最高有效数字替换为0
    • 如果第一个偶数不为零,则将该数字减 1。用9替换所有后续数字。
  3. 通过以下步骤计算最接近N的较大数字:
    • 找到第一个偶数的位置,即pos
    • pos处的数字加 1。
    • 迭代后续数字并将它们递增1。
  4. 比较与N获得的各个最接近数字的绝对差值,并打印出差值最小的那个。
  5. 如果发现两者的差异相等,则打印较小的数字。

下面是上述方法的实现:

Python
# Python program to implement
# the above approach
  
# Function to return the smaller
# number closest to N made up of
# only odd digits
def closest_smaller(N):
  
    N = str(N)
  
    l = list(map(int, N))
    length = len(l)
      
    # Stores the position of
    # first even digit of N
    pos = -1
  
    # Iterate through each digit of N
    for i in range(length):
          
        # Check for even digit.
        if l[i] % 2 == 0:
            pos = i
            break
  
    # If the first even digit is 0
    if l[pos] == 0:
  
        # Replace 0 with 9
        l[pos] = 9
  
        # Iterate over preceding
        # digits
        for i in range(pos - 1, -1, -1):
  
            # If current digit is 1
            if l[i] == 1:
  
                # Check if it is the 
                # first digit or not
                if i == 0:
                      
                    # Append leading 0's
                    l[i] = 0
                    break
                      
                # Otherwise, replace by 9
                l[i] = 9
                  
            # Otherwise
            else:
  
                # Decrease its value by 2
                l[i] -= 2
                break
  
    # If the first even digit exceeds 0
    else:
          
        # Reduce the digit by 1
        l[pos] -= 1
  
    # Replace all succeeding digits by 9
    for i in range(pos + 1, length):
        l[i] = 9
  
    # Remove leading 0s
    if l[0] == 0:
        l.pop(0)
      
      
    result = ''.join(map(str, l))
    return result
      
# Function to return the greater
# number closest to N made up of
# only odd digits
def closest_greater(N):
  
    N = str(N)
  
    l = list(map(int, N))
    length = len(l)
  
    # Stores the position of
    # first even digit of N
    pos = -1
  
    # Iterate over each digit
    # of N
    for i in range(length):
          
        # If even digit is found
        if l[i] % 2 == 0:
            pos = i
            break
              
    # Increase value of first 
    # even digit by 1
    l[pos] += 1
  
    for i in range(pos + 1, length):
        l[i] = 1
  
    result = ''.join(map(str, l))
    return result
  
# Function to check if all 
# digits of N are odd or not
def check_all_digits_odd(N):
  
    N = str(N)
  
    l = list(map(int, N))
    length = len(l)
  
    # Stores the position of
    # first even digit of N
    pos = -1
  
    # Iterating over each digit
    # of N
    for i in range(length):
          
        # If even digit is found
        if l[i] % 2 == 0:
            pos = i
            break
              
    # If no even digit is found
    if pos == -1:
        return True
    return False
  
# Function to return the 
# closest number to N
# having odd digits only
def closestNumber(N):
      
    # If all digits of N are odd
    if check_all_digits_odd(N):
        print(N)
    else:
          
        # Find smaller number 
        # closest to N
        l = int(closest_smaller(N))
          
        # Find greater number 
        # closest to N
        r = int(closest_greater(N))
          
        # Print the number with least
        # absolute difference
        if abs(N - l) <= abs(N - r):
            print(l)
        else:
            print(r)
  
# Driver Code.
if __name__ == '__main__':
      
    N = 110
    closestNumber(N)


输出:
111

时间复杂度: O(len(N))
辅助空间: O(len(N))