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

📅  最后修改于: 2022-05-13 01:57:06.557000             🧑  作者: Mango

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

给定一个数字作为字符串,从给定的字符串中删除重复出现的数字。必须就地进行更改。预期时间复杂度 O(n) 和辅助空间 O(1)。
例子:

Input:  num[] = "1299888833"
Output: num[] = "12983"

Input:  num[] = "1299888833222"
Output: num[] = "129832"

我们强烈建议您最小化您的浏览器并先自己尝试
这个问题类似于运行长度编码。

Let num[] be input number represented as character array

1) Initialize index of modified string 'j' as 0. 
2) Traverse input string and do following for every digit num[i].
   a) Copy current character 'num[i]' to 'num[j]' and increment i & j.
   b) Keep incrementing i while num[i] is same as previous digit.
3) Add string termination character at 'num[j]'

下面是上述算法的实现。

C++
// C++ program to remove recurring digits from
// a given number
#include 
using namespace std;
 
/* Removes recurring digits in num[]  */
void removeRecurringDigits(char num[])
{
    int len = strlen(num);
 
    int j = 0; // Index in modified string
 
    /* Traverse digits of given number one by one */
    for (int i=0; i


Java
// Java program to remove recurring
// digits from a given number
class GFG
{
 
    /* Removes recurring digits in num[] */
    static String removeRecurringDigits(char num[])
    {
        int len = num.length;
 
        int j = 0; // Index in modified string
        String s = "";
         
        /* Traverse digits of given number one by one */
        for (int i = 0; i < len; i++)
        {
             
            /* Copy the first occurrence of new digit */
            s += String.valueOf(num[i]);
 
            /* Remove repeating occurrences of digit */
            while (i + 1 < len && num[i] == num[i + 1])
            {
                i++;
            }
        }
        return s;
    }
 
    /* Driver code */
    public static void main(String[] args)
    {
        char num[] = "1299888833".toCharArray();
        System.out.print("Modified number is " +
                        removeRecurringDigits(num));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python3 program to remove recurring
# digits from a given number
 
# Removes recurring digits in num[]
def removeRecurringDigits(num):
 
    l = len(num)
     
    # Index in modified string
    (i, j) = (0, 0)
    str = ''
     
    # Traverse digits of given
    # number one by one
    while i < l:
         
        # Copy the first occurrence
        # of new digit
        str += num[i]
        j += 1
         
        # Remove repeating occurrences of digit
        while (i + 1 < l and num[i] == num[i + 1]):
            i += 1
        i += 1
 
    return str
 
# Driver code
if __name__ == '__main__':
     
    num = '1299888833'
    print('Modified number is {}'.format(
           removeRecurringDigits(num)))
 
# This code is contributed by rutvik_56


C#
// C# program to remove recurring
// digits from a given number
using System;
 
class GFG
{
 
    /* Removes recurring digits in num[] */
    static String removeRecurringDigits(char []num)
    {
        int len = num.Length;
 
        int j = 0; // Index in modified string
        String s = "";
         
        /* Traverse digits of given number one by one */
        for (int i = 0; i < len; i++)
        {
             
            /* Copy the first occurrence of new digit */
            s += String.Join("",num[i]);
 
            /* Remove repeating occurrences of digit */
            while (i + 1 < len && num[i] == num[i + 1])
            {
                i++;
            }
        }
        return s;
    }
 
    /* Driver code */
    public static void Main()
    {
        char []num = "1299888833".ToCharArray();
        Console.Write("Modified number is " +
                        removeRecurringDigits(num));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


输出:

Modified number is 12983