从给定的数字数组中查找所有不同的三位数字
给定一个包含digits[]的数组,其中每个元素都是一个数字整数。该数组可能包含重复项。任务是找到所有满足给定要求的唯一整数:
- 整数由任意顺序的数字的三个元素的串联组成。
- 整数没有前导零。
例子:
Input: digits[] = {2, 1, 3, 0}
Output: {102, 103, 120, 123, 130, 132, 201, 203, 210, 213, 230, 231, 301, 302, 310, 312, 320, 321}
Explanation: The above are the three digit numbers formed.
Input: digits[] = {3, 7, 5}
Output: [357, 375, 537, 573, 735, 753 ]
方法:这个问题可以通过使用频率图来解决。查找给定数字数组中所有元素的计数。请按照以下步骤解决给定的问题。
- 检查100 到 999之间的所有数字是否可以由数字向量中存在的数字组成。
- 使用2张地图相同。如果可以制作数字,则将其添加到答案中。
- 最后,返回答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all the unique
// 3 digit number that can be formed
// from the given digits
vector find3DigitNumbers(vector&
digits)
{
// Generating frequency map
// of the given digits
vector count(10, 0);
for (auto& d : digits)
count[d]++;
vector res;
for (int num = 100; num < 999; num++) {
// Generating frequency map
// of the current number
vector currCount(10, 0);
int temp = num;
while (temp) {
currCount[temp % 10]++;
temp /= 10;
}
// Checking if the number
// can be generated or not
bool flag = true;
for (int i = 0; i < 10; i++) {
if (currCount[i] > count[i]) {
flag = false;
break;
}
}
if (flag) {
res.push_back(num);
}
}
return res;
}
// Function to print answer
void printAnswer(vector& v1)
{
for (int i = 0; i < v1.size(); i++) {
cout << v1[i] << " ";
}
cout << endl;
}
// Driver code
int main()
{
vector v1 = { 2, 1, 3, 0 };
// Function Call
vector ans = find3DigitNumbers(v1);
// Printing answer
printAnswer(ans);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find all the unique
// 3 digit number that can be formed
// from the given digits
static void find3DigitNumbers(int digits[], List res)
{
// Generating frequency map
// of the given digits
int count[] = new int[10];;
for (int i = 0; i < digits.length; i++)
count[digits[i]]++;
for (int num = 100; num < 999; num++) {
// Generating frequency map
// of the current number
int currCount[] = new int[10];
int temp = num;
while (temp > 0) {
currCount[temp % 10]++;
temp /= 10;
}
// Checking if the number
// can be generated or not
Boolean flag = true;
for (int i = 0; i < 10; i++) {
if (currCount[i] > count[i]) {
flag = false;
break;
}
}
if (flag == true) {
res.add(num);
}
}
}
// Function to print answer
static void printAnswer(List res)
{
for(int i = 0; i < res.size(); i++)
System.out.print(res.get(i) + " ");
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 2, 1, 3, 0 };
List ans=new ArrayList();
// Function Call
find3DigitNumbers(arr, ans);
// Printing answer
printAnswer(ans);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python code for the above approach
# Function to find all the unique
# 3 digit number that can be formed
# from the given digits
def find3DigitNumbers(digits):
# Generating frequency map
# of the given digits
count = [0] * 10
for d in digits:
count[d] += 1
res = []
for num in range(100, 999):
# Generating frequency map
# of the current number
currCount = [0] * 10
temp = num
while (temp):
currCount[temp % 10] += 1
temp = temp // 10
# Checking if the number
# can be generated or not
flag = True
for i in range(10):
if (currCount[i] > count[i]):
flag = False
break
if (flag):
res.append(num)
return res
# Function to print answer
def printAnswer(v1):
for i in range(len(v1)):
print(v1[i], end=" ")
print('')
# Driver code
v1 = [2, 1, 3, 0]
# Function Call
ans = find3DigitNumbers(v1)
# Printing answer
printAnswer(ans)
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG {
// Function to find all the unique
// 3 digit number that can be formed
// from the given digits
static void find3DigitNumbers(int []digits, ArrayList res)
{
// Generating frequency map
// of the given digits
int []count = new int[10];;
for (int i = 0; i < digits.Length; i++)
count[digits[i]]++;
for (int num = 100; num < 999; num++) {
// Generating frequency map
// of the current number
int []currCount = new int[10];
int temp = num;
while (temp > 0) {
currCount[temp % 10]++;
temp /= 10;
}
// Checking if the number
// can be generated or not
bool flag = true;
for (int i = 0; i < 10; i++) {
if (currCount[i] > count[i]) {
flag = false;
break;
}
}
if (flag == true) {
res.Add(num);
}
}
}
// Function to print answer
static void printAnswer(ArrayList res)
{
for(int i = 0; i < res.Count; i++)
Console.Write(res[i] + " ");
}
// Driver code
public static void Main ()
{
int []arr = { 2, 1, 3, 0 };
ArrayList ans=new ArrayList();
// Function Call
find3DigitNumbers(arr, ans);
// Printing answer
printAnswer(ans);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
102 103 120 123 130 132 201 203 210 213 230 231 301 302 310 312 320 321
时间复杂度: O(1000)
辅助空间: O(N)