给定一个整数作为输入,并将整数中的所有“ 0”替换为“ 5”。
例子:
Input: 102
Output: 152
Explantion: All the digits which are '0' is replaced by '5'
Input: 1020
Output: 1525
Explantion: All the digits which are '0' is replaced by '5'
不允许使用数组存储所有数字。
资料来源:亚马逊采访体验|设置136(对于SDE-T)
迭代方法:通过观察测试用例,可以清楚地看到所有0位都由5代替。例如,对于input = 1020,output = 1525,可以写成1020 + 505,可以进一步写成1020 + 5 *(10 ^ 2)+ 5 *(10 ^ 0)。因此,可以以迭代方式形成解决方案,如果遇到“ 0”数字,则找到该数字的位置值,然后将其乘以5,然后找到数字中所有0的总和。将该总和加到输入数字中以找到输出数字。
算法:
- 创建一个变量sum = 0以存储总和, place = 1以存储当前数字的位值,并创建输入变量的副本
- 如果数字为零,则返回5
- 输入变量大于0时,迭代下一步
- 提取最后一位(n%10) ,如果该位为零,则更新sum = sum + place * 5 ,从数字n = n / 10中删除最后一位,并更新place = place * 10
- 返回总和。
C++
#include
using namespace std;
// Returns the number to be added to the
// input to replace all zeroes with five
int calculateAddedValue(int number)
{
// Amount to be added
int result = 0;
// Unit decimal place
int decimalPlace = 1;
if (number == 0)
{
result += (5 * decimalPlace);
}
while (number > 0)
{
if (number % 10 == 0)
{
// A number divisible by 10, then
// this is a zero occurrence in
// the input
result += (5 * decimalPlace);
}
// Move one decimal place
number /= 10;
decimalPlace *= 10;
}
return result;
}
int replace0with5(int number)
{
return number += calculateAddedValue(number);
}
// Driver code
int main()
{
cout << replace0with5(1020);
}
// This code is contributed by avanitrachhadiya2155
Java
public class ReplaceDigits {
static int replace0with5(int number)
{
return number += calculateAddedValue(number);
}
// returns the number to be added to the
// input to replace all zeroes with five
private static int calculateAddedValue(int number)
{
// amount to be added
int result = 0;
// unit decimal place
int decimalPlace = 1;
if (number == 0) {
result += (5 * decimalPlace);
}
while (number > 0) {
if (number % 10 == 0)
// a number divisible by 10, then
// this is a zero occurrence in the input
result += (5 * decimalPlace);
// move one decimal place
number /= 10;
decimalPlace *= 10;
}
return result;
}
public static void main(String[] args)
{
System.out.print(replace0with5(1020));
}
}
Python3
def replace0with5(number):
number += calculateAddedValue(number)
return number
# returns the number to be added to the
# input to replace all zeroes with five
def calculateAddedValue(number):
# amount to be added
result = 0
# unit decimal place
decimalPlace = 1
if (number == 0):
result += (5 * decimalPlace)
while (number > 0):
if (number % 10 == 0):
# a number divisible by 10, then
# this is a zero occurrence in the input
result += (5 * decimalPlace)
# move one decimal place
number //= 10
decimalPlace *= 10
return result
# Driver code
print(replace0with5(1020))
# This code is contributed by shubhmasingh10
C#
using System;
class GFG{
static int replace0with5(int number)
{
return number += calculateAddedValue(number);
}
// Returns the number to be added to the
// input to replace all zeroes with five
static int calculateAddedValue(int number)
{
// Amount to be added
int result = 0;
// Unit decimal place
int decimalPlace = 1;
if (number == 0)
{
result += (5 * decimalPlace);
}
while (number > 0)
{
if (number % 10 == 0)
// A number divisible by 10, then
// this is a zero occurrence in the input
result += (5 * decimalPlace);
// Move one decimal place
number /= 10;
decimalPlace *= 10;
}
return result;
}
// Driver Code
static public void Main()
{
Console.WriteLine(replace0with5(1020));
}
}
// This code is contributed by rag2127
Javascript
C++
// C++ program to replace all ‘0’
// with ‘5’ in an input Integer
#include
using namespace std;
// A recursive function to replace all 0s
// with 5s in an input number It doesn't
// work if input number itself is 0.
int convert0To5Rec(int num)
{
// Base case for recursion termination
if (num == 0)
return 0;
// Extraxt the last digit and
// change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits and
// append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls convert0To5Rec()
// for other numbers
int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver code
int main()
{
int num = 10120;
cout << convert0To5(num);
return 0;
}
// This code is contributed by Code_Mech.
C
// C program to replace all ‘0’
// with ‘5’ in an input Integer
#include
// A recursive function to replace
// all 0s with 5s in an input number
// It doesn't work if input number itself is 0.
int convert0To5Rec(int num)
{
// Base case for recursion termination
if (num == 0)
return 0;
// Extract the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits
// and append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls
// convert0To5Rec() for other numbers
int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver program to test above function
int main()
{
int num = 10120;
printf("%d", convert0To5(num));
return 0;
}
Java
// Java code for Replace all 0 with
// 5 in an input Integer
class GFG {
// A recursive function to replace all 0s with 5s in
// an input number. It doesn't work if input number
// itself is 0.
static int convert0To5Rec(int num)
{
// Base case
if (num == 0)
return 0;
// Extraxt the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits and append the
// last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls convert0To5Rec() for
// other numbers
static int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver function
public static void main(String[] args)
{
System.out.println(convert0To5(10120));
}
}
// This code is contributed by Kamal Rawal
Python
# Python program to replace all
# 0 with 5 in given integer
# A recursive function to replace all 0s
# with 5s in an integer
# Does'nt work if the given number is 0 itself
def convert0to5rec(num):
# Base case for recurssion termination
if num == 0:
return 0
# Extract the last digit and change it if needed
digit = num % 10
if digit == 0:
digit = 5
# Convert remaining digits and append the last digit
return convert0to5rec(num / 10) * 10 + digit
# It handles 0 to 5 calls convert0to5rec()
# for other numbers
def convert0to5(num):
if num == 0:
return 5
else:
return convert0to5rec(num)
# Driver Program
num = 10120
print convert0to5(num)
# Contributed by Harshit Agrawal
C#
// C# code for Replace all 0
// with 5 in an input Integer
using System;
class GFG {
// A recursive function to replace
// all 0s with 5s in an input number.
// It doesn't work if input number
// itself is 0.
static int convert0To5Rec(int num)
{
// Base case
if (num == 0)
return 0;
// Extraxt the last digit and
// change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits
// and append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls
// convert0To5Rec() for other numbers
static int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver Code
static public void Main()
{
Console.Write(convert0To5(10120));
}
}
// This code is contributed by Raj
PHP
输出:
1525
复杂度分析:
- 时间复杂度: O(k),循环仅运行k次,其中k是该数字的位数。
- 空间复杂度: O(1),不需要额外的空间。
递归方法:这个想法很简单,我们使用mod运算符’%’获得最后一位数字。如果数字为0,我们将其替换为5,否则,请保持原样。然后,我们再次查询剩余的数字。方法保持不变,基本区别是循环被递归函数代替。
算法:
- 当数字为0时,请检查一个基本情况是否返回5,所有其他情况都构成一个递归函数。
- 函数( solve(int n) )可以定义如下,如果传递的数字为0,则返回0,否则提取最后一位,即n = n / 10并删除最后一位。如果最后一位数字为零,则为其分配5。
- 现在,通过为n调用递归函数来返回值,即返回resolve(n)* 10 + digit 。
- 打印答案。
C++
// C++ program to replace all ‘0’
// with ‘5’ in an input Integer
#include
using namespace std;
// A recursive function to replace all 0s
// with 5s in an input number It doesn't
// work if input number itself is 0.
int convert0To5Rec(int num)
{
// Base case for recursion termination
if (num == 0)
return 0;
// Extraxt the last digit and
// change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits and
// append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls convert0To5Rec()
// for other numbers
int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver code
int main()
{
int num = 10120;
cout << convert0To5(num);
return 0;
}
// This code is contributed by Code_Mech.
C
// C program to replace all ‘0’
// with ‘5’ in an input Integer
#include
// A recursive function to replace
// all 0s with 5s in an input number
// It doesn't work if input number itself is 0.
int convert0To5Rec(int num)
{
// Base case for recursion termination
if (num == 0)
return 0;
// Extract the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits
// and append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls
// convert0To5Rec() for other numbers
int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver program to test above function
int main()
{
int num = 10120;
printf("%d", convert0To5(num));
return 0;
}
Java
// Java code for Replace all 0 with
// 5 in an input Integer
class GFG {
// A recursive function to replace all 0s with 5s in
// an input number. It doesn't work if input number
// itself is 0.
static int convert0To5Rec(int num)
{
// Base case
if (num == 0)
return 0;
// Extraxt the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits and append the
// last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls convert0To5Rec() for
// other numbers
static int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver function
public static void main(String[] args)
{
System.out.println(convert0To5(10120));
}
}
// This code is contributed by Kamal Rawal
Python
# Python program to replace all
# 0 with 5 in given integer
# A recursive function to replace all 0s
# with 5s in an integer
# Does'nt work if the given number is 0 itself
def convert0to5rec(num):
# Base case for recurssion termination
if num == 0:
return 0
# Extract the last digit and change it if needed
digit = num % 10
if digit == 0:
digit = 5
# Convert remaining digits and append the last digit
return convert0to5rec(num / 10) * 10 + digit
# It handles 0 to 5 calls convert0to5rec()
# for other numbers
def convert0to5(num):
if num == 0:
return 5
else:
return convert0to5rec(num)
# Driver Program
num = 10120
print convert0to5(num)
# Contributed by Harshit Agrawal
C#
// C# code for Replace all 0
// with 5 in an input Integer
using System;
class GFG {
// A recursive function to replace
// all 0s with 5s in an input number.
// It doesn't work if input number
// itself is 0.
static int convert0To5Rec(int num)
{
// Base case
if (num == 0)
return 0;
// Extraxt the last digit and
// change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// Convert remaining digits
// and append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
// It handles 0 and calls
// convert0To5Rec() for other numbers
static int convert0To5(int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
// Driver Code
static public void Main()
{
Console.Write(convert0To5(10120));
}
}
// This code is contributed by Raj
的PHP
输出:
15125
复杂度分析:
- 时间复杂度: O(k),重新函数仅调用k次,其中k是该数字的位数
- 空间复杂度: O(1),不需要额外的空间。