在中间插入成对绝对差后,通过从两端选择最大值来创建新的手机号码
给定一个字符串ph[] ,任务是找到连续元素的绝对差并将结果插入连续元素之间。通过这样做,电话号码的大小将从10增加到19 。现在我们必须比较第一个和最后一个数字并选择最大值,这样我们将获得一个新的电话号码。
例子:
Input: ph = “9647253846”
Output: 9364857553
Explanation:
Input: ph = “7635892563”
Output: 7363535791
方法:这个想法是使用两个指针的方法来解决这个问题,同时使用一个字符串变量来添加不同的电话号码。请按照以下步骤解决问题:
- 将字符串变量S[]初始化为空字符串。
- 使用变量i遍历范围[0, N-1)并执行以下任务:
- 将ph[i]添加到变量S。
- 将变量s初始化为int(ph[i]) – int(ph[i+1])并将x初始化为abs(s)。
- 将str(x)添加到变量S。
- 将ph[9]添加到变量S。
- 将变量s初始化为0 ,将e初始化为len(S)-1。
- 将字符串变量ph2[]初始化为空字符串。
- 在while循环中遍历直到s不等于e并执行以下任务:
- 将S[s]或S[e]的最大值添加到变量ph2[]。
- 将s的值加1 ,将e的值减1。
- 将S[e]添加到变量ph2[]。
- 执行上述步骤后,打印ph2[]的值作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to create the phone number
string phNumber(string ph, int n)
{
// Empty string to store extended number
string S = "";
// Loop for extend phone number
for (int i = 0; i < n - 1; i++) {
// Add character form string
S += ph[i];
// Finding absolute difference
int x = abs(ph[i] - ph[i + 1]);
// Adding the difference to S
S += to_string(x);
}
// Adding last digit of ph
S += ph[9];
// Using 2 pointer algorithm to form comparison
// s is start index
int s = 0;
// e is end index
int e = S.length() - 1;
// ph2 an empty string for
// storing the the phone number
string ph2 = "";
// Loop till e become equal to s
while (s != e) {
// Comparing element at s and e
// index and adding maximum
if (S[s] > S[e]) {
ph2 += S[s];
}
else {
ph2 += S[e];
}
// Increment start index
s += 1;
// Decrement end index
e -= 1;
}
// When s = e loop will terminate
// so adding element at index s = e
ph2 += S[e];
// Return phone number
return ph2;
}
// Driver Code
int main()
{
// Original phone number
string ph = "9647253846";
// Calling function to create new number
string num = phNumber(ph, ph.length());
// Print the new number
cout << num;
}
// This code is contributed by Samim Hossain Mondal.
Java
// Java program for the above approach
class GFG {
// Function to create the phone number
static String phNumber(String ph, int n) {
// Empty string to store extended number
String S = "";
// Loop for extend phone number
for (int i = 0; i < n - 1; i++) {
// Add character form string
S += ph.charAt(i);
// Finding absolute difference
int x = Math.abs(ph.charAt(i) - ph.charAt(i + 1));
// Adding the difference to S
S += Integer.toString(x);
}
// Adding last digit of ph
S += ph.charAt(9);
// Using 2 pointer algorithm to form comparison
// s is start index
int s = 0;
// e is end index
int e = S.length() - 1;
// ph2 an empty string for
// storing the the phone number
String ph2 = "";
// Loop till e become equal to s
while (s != e) {
// Comparing element at s and e
// index and adding maximum
if (S.charAt(s) > S.charAt(e)) {
ph2 += S.charAt(s);
} else {
ph2 += S.charAt(e);
}
// Increment start index
s += 1;
// Decrement end index
e -= 1;
}
// When s = e loop will terminate
// so adding element at index s = e
ph2 += S.charAt(e);
// Return phone number
return ph2;
}
// Driver Code
public static void main(String args[]) {
// Original phone number
String ph = "9647253846";
// Calling function to create new number
String num = phNumber(ph, ph.length());
// Print the new number
System.out.println(num);
}
}
// This code is contributed by gfgking
Python3
# Function to create the phone number
def phNumber(ph, n):
# Empty string to store extended number
S =""
# Loop for extend phone number
for i in range(n-1):
# Add character form string
S+= ph[i]
# Finding absolute difference
s = int(ph[i])-int(ph[i + 1])
x = abs(s)
# Adding the difference to S
S+= str(x)
# Adding last digit of ph
S+= ph[9]
# Using 2 pointer algorithm to form comparison
# s is start index
s = 0
# e is end index
e = len(S)-1
# ph2 an empty string for
# storing the the phone number
ph2 =""
# Loop till e become equal to s
while s != e:
# Comparing element at s and e
# index and adding maximum
ph2+= max(S[s], S[e])
# Increment start index
s+= 1
# Decrement end index
e-= 1
# When s = e loop will terminate
# so adding element at index s = e
ph2+= S[e]
# Return phone number
return ph2
# Original phone number
ph = "9647253846"
# Calling function to create new number
num = phNumber(ph, len(ph))
# Print the new number
print(num)
C#
// C# program for the above approach
using System;
class GFG {
// Function to create the phone number
static string phNumber(string ph, int n)
{
// Empty string to store extended number
string S = "";
// Loop for extend phone number
for (int i = 0; i < n - 1; i++) {
// Add character form string
S += ph[i];
// Finding absolute difference
int x = Math.Abs(ph[i] - ph[i + 1]);
// Adding the difference to S
S += x.ToString();
}
// Adding last digit of ph
S += ph[9];
// Using 2 pointer algorithm to form comparison
// s is start index
int s = 0;
// e is end index
int e = S.Length - 1;
// ph2 an empty string for
// storing the the phone number
string ph2 = "";
// Loop till e become equal to s
while (s != e) {
// Comparing element at s and e
// index and adding maximum
if (S[s] > S[e]) {
ph2 += S[s];
}
else {
ph2 += S[e];
}
// Increment start index
s += 1;
// Decrement end index
e -= 1;
}
// When s = e loop will terminate
// so adding element at index s = e
ph2 += S[e];
// Return phone number
return ph2;
}
// Driver Code
public static void Main()
{
// Original phone number
string ph = "9647253846";
// Calling function to create new number
string num = phNumber(ph, ph.Length);
// Print the new number
Console.Write(num);
}
}
// This code is contributed by ukasp.
Javascript
输出
9364857553
时间复杂度: O(N)
辅助空间: O(N)