📜  删除给定数字中的重复数字

📅  最后修改于: 2021-05-05 00:25:53             🧑  作者: Mango

给定一个整数,从中删除连续的重复数字。

例子:

Input: x = 12224
Output: 124

Input: x = 124422
Output: 1242

Input: x = 11332
Output: 132

我们需要处理n的所有数字并删除连续的表示形式。我们可以通过将n除以10并取n%10来遍历所有数字。

C++
// C++ program to remove repeated digits
#include 
using namespace std;
  
long int removeRecur(long int n)
{
    // Store first digits as previous digit
    int prev_digit = n % 10;
  
    // Initialize power
    long int pow = 10;
    long int res = prev_digit;
  
    // Iterate through all digits of n, note that
    // the digits are processed from least significant
    // digit to most significant digit.
    while (n) {
        // Store current digit
        int curr_digit = n % 10;
  
        if (curr_digit != prev_digit) {
            // Add the current digit to the beginning
            // of result
            res += curr_digit * pow;
  
            // Update previous result and power
            prev_digit = curr_digit;
            pow *= 10;
        }
  
        // Remove last digit from n
        n = n / 10;
    }
  
    return res;
}
  
// Driver program
int main()
{
    long int n = 12224;
    cout << removeRecur(n);
    return 0;
}


Java
// Java program to remove repeated digits
import java.io.*;
  
class GFG {
  
    static long removeRecur(long n)
    {
          
        // Store first digits as previous
        // digit
        long prev_digit = n % 10;
      
        // Initialize power
        long pow = 10;
        long res = prev_digit;
      
        // Iterate through all digits of n,
        // note that the digits are 
        // processed from least significant
        // digit to most significant digit.
        while (n>0) {
              
            // Store current digit
            long curr_digit = n % 10;
      
            if (curr_digit != prev_digit) 
            {
                // Add the current digit to
                // the beginning of result
                res += curr_digit * pow;
      
                // Update previous result
                // and power
                prev_digit = curr_digit;
                pow *= 10;
            }
      
            // Remove last digit from n
            n = n / 10;
        }
      
        return res;
    }
      
    // Driver program
    public static void main (String[] args)
    {
        long n = 12224;
          
        System.out.println(removeRecur(n));
    }
}
  
// This code is contributed by anuj_67.


Python3
# Python 3 program to remove repeated digits
  
def removeRecur(n):
      
    # Store first digits as previous digit
    prev_digit = n % 10
  
    # Initialize power
    pow = 10
    res = prev_digit
  
    # Iterate through all digits of n, note 
    # that the digits are processed from 
    # least significant digit to most 
    # significant digit.
    while (n):
          
        # Store current digit
        curr_digit = n % 10
  
        if (curr_digit != prev_digit):
              
            # Add the current digit to the 
            # beginning of result
            res += curr_digit * pow
  
            # Update previous result and power
            prev_digit = curr_digit
            pow *= 10
  
        # Remove last digit from n
        n = int(n / 10)
      
    return res
  
# Driver Code
if __name__ == '__main__':
    n = 12224
    print(removeRecur(n))
  
# This code is contributed by 
# Surendra_Gangwar


C#
// C# program to remove repeated digits
using System;
  
class GFG {
  
    static long removeRecur(long n)
    {
          
        // Store first digits as previous
        // digit
        long prev_digit = n % 10;
      
        // Initialize power
        long pow = 10;
        long res = prev_digit;
      
        // Iterate through all digits of n,
        // note that the digits are 
        // processed from least significant
        // digit to most significant digit.
        while (n > 0) {
              
            // Store current digit
            long curr_digit = n % 10;
      
            if (curr_digit != prev_digit) 
            {
                // Add the current digit to
                // the beginning of result
                res += curr_digit * pow;
      
                // Update previous result
                // and power
                prev_digit = curr_digit;
                pow *= 10;
            }
      
            // Remove last digit from n
            n = n / 10;
        }
      
        return res;
    }
      
    // Driver program
    public static void Main ()
    {
        long n = 12224;
          
        Console.WriteLine(removeRecur(n));
    }
}
  
// This code is contributed by anuj_67.


PHP


输出:

124