通过重新排列给定正数或负数的数字来获得最大数
给定一个整数N (正数或负数),任务是找到可以使用该数的所有数字组成的最大数。
例子:
Input: -38290367
Output: -20336789
Explanation: As there is need to use all the digits, 0 cannot be the first digit because it becomes redundant at first position.
Input: 1203465
Output: 6543210
方法:数字中的位数范围为 0-9,因此想法是创建一个大小为 10 的哈希数组,并将该数字中出现的每个数字的计数存储在哈希数组中。请按照以下步骤解决问题:
- 然后先检查数字是正数还是负数
- 如果数字是正数,那么如本文所述,从索引9 到 0遍历散列数组并相应地计算数字。
- 但如果数字为负数,则从0-9遍历数组,这样:
- 可以有前导 0。所以,找到最小的非零数字
- 在开头插入最小的非零数字,然后按顺序将所有其他数字从 0 到 9添加到最终答案
- 然后将数字乘以 -1 使其为负数
- 返回结果数字
下面是上述方法的实现
C++
// C++ program to implement the approach
#include
using namespace std;
// Function to print the maximum number
long long printMaxNum(long long num)
{
// Initialising hash array
int hash[10] = { 0 };
long long n = num < 0 ? num * -1 : num;
long long ans = 0;
while (n) {
hash[n % 10]++;
n = n / 10;
}
// If positive number
if (num > 0) {
for (int i = 9; i >= 0; i--)
for (int j = 0; j < hash[i]; j++)
ans = ans * 10 + i;
}
// If negative number
else {
// If 0 is present in the number
if (hash[0] > 0) {
for (int i = 1; i < 10; i++)
if (hash[i] > 0) {
ans = i;
hash[i]--;
break;
}
}
for (int i = 0; i < 10; i++)
for (int j = 0; j < hash[i]; j++)
ans = ans * 10 + i;
ans = ans * -1;
}
return ans;
}
// Driver code
int main()
{
int N = -38290367;
// Function call
cout << printMaxNum(N);
return 0;
}
Java
// Java program to implement the approach
class GFG
{
// Function to print the maximum number
static long printMaxNum(long num)
{
// Initialising hash array
int[] hash = new int[10];
for (int i = 0; i < 10; i++) {
hash[i] = 0;
}
long n = num < 0 ? num * -1 : num;
long ans = 0;
while (n > 0) {
hash[(int)(n % 10)] += 1;
n = n / 10;
}
// If positive number
if (num > 0) {
for (int i = 9; i >= 0; i--)
for (int j = 0; j < hash[i]; j++)
ans = ans * 10 + i;
}
// If negative number
else {
// If 0 is present in the number
if (hash[0] > 0) {
for (int i = 1; i < 10; i++)
if (hash[i] > 0) {
ans = i;
hash[i]--;
break;
}
}
for (int i = 0; i < 10; i++)
for (int j = 0; j < hash[i]; j++)
ans = ans * 10 + i;
ans = ans * -1;
}
return ans;
}
// Driver code
public static void main(String args[])
{
int N = -38290367;
// Function call
System.out.println(printMaxNum(N));
}
}
// This code is contributed by gfgking
Python3
# Python program to implement the approach
# Function to print the maximum number
def printMaxNum(num):
# Initialising hash array
hash = []
for i in range(0, 10):
hash.append(0)
if(num < 0):
n = num * -1
else:
n = num
ans = 0
while (n != 0):
hash[int(n % 10)] = hash[int(n % 10)] + 1
n = n // 10
# If positive number
if (num > 0):
for i in range(9, -1, -1):
for j in range(0, hash[i]):
ans = ans * 10 + i
# If negative number
else:
# If 0 is present in the number
if (hash[0] > 0):
for i in range(1, 10):
if (hash[i] > 0):
ans = i
hash[i] = hash[i]-1
break
for i in range(0, 10):
for j in range(0, hash[i]):
ans = ans * 10 + i
ans = ans * -1
return ans
# Driver code
N = -38290367
# Function call
print(printMaxNum(N))
# This code is contributed by Taranpreet
C#
// C# program to implement the approach
using System;
class GFG {
// Function to print the maximum number
static long printMaxNum(long num)
{
// Initialising hash array
int[] hash = new int[10];
for (int i = 0; i < 10; i++) {
hash[i] = 0;
}
long n = num < 0 ? num * -1 : num;
long ans = 0;
while (n > 0) {
hash[n % 10]++;
n = n / 10;
}
// If positive number
if (num > 0) {
for (int i = 9; i >= 0; i--)
for (int j = 0; j < hash[i]; j++)
ans = ans * 10 + i;
}
// If negative number
else {
// If 0 is present in the number
if (hash[0] > 0) {
for (int i = 1; i < 10; i++)
if (hash[i] > 0) {
ans = i;
hash[i]--;
break;
}
}
for (int i = 0; i < 10; i++)
for (int j = 0; j < hash[i]; j++)
ans = ans * 10 + i;
ans = ans * -1;
}
return ans;
}
// Driver code
public static void Main()
{
int N = -38290367;
// Function call
Console.Write(printMaxNum(N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
-20336789
时间复杂度: O(length(N))
辅助空间: O(1)