原文:
给定一个整数 n ,任务是按排序顺序打印所有出现在 n 中的重复数字。
示例:
输入: n = 45244336543 输出: 3 4 5 说明:重复数字为 3 4 5
输入: n = 987065467809 输出: 0 6 7 8 9
方式:思路是用存储 n 的数字的
- 初始化一个 ,存储数字 0-9 的频率。
- 。
- 的字符,对于每个字符,执行以下步骤:
- 将当前字符转换为整数
- 在中增加该数字的计数。
- 打印计数超过 1 的数字。
下面是上述方法的实现:
c
// c program for the above approach
#include
using namespace std;
// function to print repeating
// digits present in the number n
void printdup(string n)
{
// stores the count of
// unique digits
int res = 0;
// map to store frequency
// of each digit
int cnt[10];
// set count of all digits to 0
for (int i = 0; i < 10; i )
cnt[i] = 0;
// traversing the string
for (int i = 0; i < n.size(); i ) {
// convert the character
// to equivalent integer
int digit = (n[i] - '0');
// increase the count of digit
cnt[digit] = 1;
}
// traverse the map
for (int i = 0; i < 10; i ) {
// if frequency
// of digit exceeds 1
if (cnt[i] > 1)
cout << i << " ";
}
cout << endl;
}
// driver code
int main()
{
string n = "45244336543";
// function call to print
// repeating digits in n
printdup(n);
return 0;
}
// this code is contributed by kingash.
java 语言(一种计算机语言,尤用于创建网站)
// java program for the above approach
import java.io.*;
import java.util.*;
class gfg {
// function to print repeating
// digits present in the number n
static void printdup(string n)
{
// stores the count of
// unique digits
int res = 0;
// map to store frequency
// of each digit
int cnt[] = new int[10];
// traversing the string
for (int i = 0; i < n.length(); i ) {
// convert the character
// to equivalent integer
int digit = (n.charat(i) - '0');
// increase the count of digit
cnt[digit] = 1;
}
// traverse the map
for (int i = 0; i < 10; i ) {
// if frequency
// of digit exceeds 1
if (cnt[i] > 1)
system.out.print(i " ");
}
system.out.println();
}
// driver code
public static void main(string[] args)
{
string n = "45244336543";
// function call to print
// repeating digits in n
printdup(n);
}
}
// this code is contributed by kingash.
python 3
# python implementation
# of the above approach
# function to print repeating
# digits present in the number n
def printdup(n):
# stores the count of
# unique digits
res = 0
# set count of all digits to 0
cnt = [0] * 10
# convert integer to
# equivalent string
string = str(n)
# traversing the string
for i in string:
# convert the character
# to equivalent integer
digit = int(i)
# increase the count of digit
cnt[digit] = 1
# traverse the map
for i in range(10):
# if frequency
# of digit is 1
if (cnt[i] > 1):
# if count exceeds 1
print(i, end=" ")
# driver code
n = 45244336543
# function call to print
# repeating digits in n
printdup(n)
c
// c# program to implement
// the above approach
using system;
class gfg
{
// function to print repeating
// digits present in the number n
static void printdup(string n)
{
// stores the count of
// unique digits
int res = 0;
// map to store frequency
// of each digit
int[] cnt = new int[10];
// traversing the string
for (int i = 0; i < n.length; i ) {
// convert the character
// to equivalent integer
int digit = (n[i] - '0');
// increase the count of digit
cnt[digit] = 1;
}
// traverse the map
for (int i = 0; i < 10; i ) {
// if frequency
// of digit exceeds 1
if (cnt[i] > 1)
console.write(i " ");
}
console.writeline();
}
// driver code
public static void main()
{
string n = "45244336543";
// function call to print
// repeating digits in n
printdup(n);
}
}
// this code is contributed by code_hunt.
java 描述语言
output:
3 4 5
时间复杂度:o(log10n) 辅助空间: o(1)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处