📜  查找与给定数字 N 具有不同数字的下一个数字

📅  最后修改于: 2021-10-27 08:28:17             🧑  作者: Mango

给定一个自然数N ,任务是找到与给定数字具有不同数字的下一个数字。
例子:

方法:这个想法是使用散列来计算下一个数字与给定数字不同的数字 –

  • 创建一个散列数组来存储数字中存在的数字,存储最重要的数字并将数字中存在的位数存储在计数变量中
  • 找到数字中不存在且大于当前最高有效位的最高有效位的下一个数字。
  • 如果未找到下一个最高有效位,则通过增加计数来增加位数,并找到 1 到 9 之间不存在于数字中的最高有效位。
  • 如果找到下一个最高有效数字,则找到下一个最小数字,该数字在 0 到 9 之间的数字中不存在。
  • 迭代从 1 到计数的位数
    • 将最高有效数字乘以 10,然后添加给定数字中不存在的下一个数字。

下面是上述方法的实现:

C++
// C++ implementation to find
// the next distinct digits number
 
#include 
using namespace std;
 
// Function to find the
// next distinct digits number
void findNextNumber(int n)
{
    int h[10] = { 0 };
    int i = 0, msb = n, rem = 0;
    int next_num = -1, count = 0;
 
    // Loop to find the distinct
    // digits using hash array
    // and the number of digits
    while (msb > 9) {
        rem = msb % 10;
        h[rem] = 1;
        msb /= 10;
        count++;
    }
    h[msb] = 1;
    count++;
 
    // Loop to find the most significant
    // distinct digit of the next number
    for (i = msb + 1; i < 10; i++) {
        if (h[i] == 0) {
            next_num = i;
            break;
        }
    }
     
    // Condition to check if the number
    // is possible with the same number
    // of digits count
    if (next_num == -1){
        for (i = 1; i < msb; i++){
            if (h[i] == 0){
                next_num = i;
                count++;
                break;
            }
        }
    }
     
    // Condition to check if the desired
    // most siginificant distinct
    // digit is found
    if (next_num > 0){
         
        // Loop to find the minimum next digit
        // which is not present in the number
        for (i = 0; i < 10; i++) {
            if (h[i] == 0) {
                msb = i;
                break;
            }
        }
         
        // Computation of the number
        for (i = 1; i < count; i++) {
            next_num = ((next_num * 10) + msb);
        }
         
        // Condition to check if the number is
        // greater than the given number
        if (next_num > n)
            cout << next_num << "\n";
        else
            cout << "Not Possible \n";
    }
    else{
        cout << "Not Possible \n";
    }
}
 
// Driver Code
int main()
{
    int n = 2019;
    findNextNumber(n);
    return 0;
}


Java
// Java implementation to find
// the next distinct digits number
class GFG{
  
// Function to find the
// next distinct digits number
static void findNextNumber(int n)
{
    int h[] = new int[10];
    int i = 0, msb = n, rem = 0;
    int next_num = -1, count = 0;
  
    // Loop to find the distinct
    // digits using hash array
    // and the number of digits
    while (msb > 9) {
        rem = msb % 10;
        h[rem] = 1;
        msb /= 10;
        count++;
    }
    h[msb] = 1;
    count++;
  
    // Loop to find the most significant
    // distinct digit of the next number
    for (i = msb + 1; i < 10; i++) {
        if (h[i] == 0) {
            next_num = i;
            break;
        }
    }
      
    // Condition to check if the number
    // is possible with the same number
    // of digits count
    if (next_num == -1){
        for (i = 1; i < msb; i++){
            if (h[i] == 0){
                next_num = i;
                count++;
                break;
            }
        }
    }
      
    // Condition to check if the desired
    // most siginificant distinct
    // digit is found
    if (next_num > 0){
          
        // Loop to find the minimum next digit
        // which is not present in the number
        for (i = 0; i < 10; i++) {
            if (h[i] == 0) {
                msb = i;
                break;
            }
        }
          
        // Computation of the number
        for (i = 1; i < count; i++) {
            next_num = ((next_num * 10) + msb);
        }
          
        // Condition to check if the number is
        // greater than the given number
        if (next_num > n)
            System.out.print(next_num+ "\n");
        else
            System.out.print("Not Possible \n");
    }
    else{
        System.out.print("Not Possible \n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 2019;
    findNextNumber(n);
}
}
 
// This code is contributed by 29AjayKumar


C#
// C# implementation to find
// the next distinct digits number
using System;
 
class GFG{
  
    // Function to find the
    // next distinct digits number
    static void findNextNumber(int n)
    {
        int []h = new int[10];
        int i = 0, msb = n, rem = 0;
        int next_num = -1, count = 0;
      
        // Loop to find the distinct
        // digits using hash array
        // and the number of digits
        while (msb > 9) {
            rem = msb % 10;
            h[rem] = 1;
            msb /= 10;
            count++;
        }
        h[msb] = 1;
        count++;
      
        // Loop to find the most significant
        // distinct digit of the next number
        for (i = msb + 1; i < 10; i++) {
            if (h[i] == 0) {
                next_num = i;
                break;
            }
        }
          
        // Condition to check if the number
        // is possible with the same number
        // of digits count
        if (next_num == -1){
            for (i = 1; i < msb; i++){
                if (h[i] == 0){
                    next_num = i;
                    count++;
                    break;
                }
            }
        }
          
        // Condition to check if the desired
        // most siginificant distinct
        // digit is found
        if (next_num > 0){
              
            // Loop to find the minimum next digit
            // which is not present in the number
            for (i = 0; i < 10; i++) {
                if (h[i] == 0) {
                    msb = i;
                    break;
                }
            }
              
            // Computation of the number
            for (i = 1; i < count; i++) {
                next_num = ((next_num * 10) + msb);
            }
              
            // Condition to check if the number is
            // greater than the given number
            if (next_num > n)
                Console.WriteLine(next_num);
            else
                Console.WriteLine("Not Possible");
        }
        else{
            Console.WriteLine("Not Possible");
        }
    }
      
    // Driver Code
    public static void Main(string[] args)
    {
        int n = 2019;
        findNextNumber(n);
    }
}
 
// This code is contributed by Yash_R


Python 3
# Python 3 implementation to find
# the next distinct digits number
 
# Function to find the
# next distinct digits number
def findNextNumber(n):
    h = [0 for i in range(10)]
    i = 0
    msb = n
    rem = 0
    next_num = -1
    count = 0
 
    # Loop to find the distinct
    # digits using hash array
    # and the number of digits
    while (msb > 9):
        rem = msb % 10
        h[rem] = 1
        msb //= 10
        count += 1
 
    h[msb] = 1
    count += 1
 
    # Loop to find the most significant
    # distinct digit of the next number
    for i in range(msb + 1,10,1):
        if (h[i] == 0):
            next_num = i
            break
     
    # Condition to check if the number
    # is possible with the same number
    # of digits count
    if (next_num == -1):
        for i in range(1,msb,1):
            if (h[i] == 0):
                next_num = i
                count += 1
                break
     
    # Condition to check if the desired
    # most siginificant distinct
    # digit is found
    if (next_num > 0):
         
                # Loop to find the minimum next digit
        # which is not present in the number
        for i in range(0,10,1):
            if (h[i] == 0):
                msb = i
                break
         
        # Computation of the number
        for i in range(1,count,1):
            next_num = ((next_num * 10) + msb)
         
        # Condition to check if the number is
        # greater than the given number
        if (next_num > n):
            print(next_num)
        else:
            print("Not Possible")
    else:
        print("Not Possible")
 
# Driver Code
if __name__ == '__main__':
    n = 2019
    findNextNumber(n)
 
# This code is contributed by Surendra_Gangwar


Javascript


输出:
3333

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程