📜  从两个数字的总和中删除尾随零(使用堆栈)

📅  最后修改于: 2021-09-05 11:49:15             🧑  作者: Mango

给定两个数字AB ,任务是使用堆栈删除两个给定数字之和中存在的尾随零。

例子:

方法:可以使用字符串和堆栈数据结构解决给定的问题。 请按照以下步骤解决问题:

  • 计算A + B并将其存储在一个变量中,例如N
  • 初始化一个堆栈 ,比如说S ,来存储N的数字。
  • 将整数N转换为字符串,然后将字符压入堆栈S
  • 迭代while S不为空(),如果栈顶元素为’0’,则将其从栈中弹出。否则,断。
  • 初始化一个字符串,比如res ,以存储结果字符串。
  • 迭代而 S 不为空(),将res 中的所有字符压入,然后弹出顶部元素。
  • 反转字符串res并打印res作为答案。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to remove trailing
// zeros from the sum of two numbers
string removeTailing(int A, int B)
{
    // Stores the sum of A and B
    int N = A + B;
 
    // Stores the digits
    stack s;
 
    // Stores the equivalent
    // string of integer N
    string strsum = to_string(N);
 
    // Traverse the string
    for (int i = 0; i < strsum.length(); i++) {
 
        // Push the digit at i
        // in the stack
        s.push(strsum[i]);
    }
 
    // While top element is '0'
    while (s.top() == '0')
 
        // Pop the top element
        s.pop();
 
    // Stores the resultant number
    // without tailing 0's
    string res = "";
 
    // While s is not empty
    while (!s.empty()) {
 
        // Append top element of S in res
        res = res + char(s.top());
 
        // Pop the top element of S
        s.pop();
    }
 
    // Reverse the string res
    reverse(res.begin(), res.end());
 
    return res;
}
 
// Driver Code
int main()
{
    // Input
    int A = 130246, B = 450164;
 
    // Function Call
    cout << removeTailing(A, B);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to remove trailing
// zeros from the sum of two numbers
static String removeTailing(int A, int B)
{
     
    // Stores the sum of A and B
    int N = A + B;
 
    // Stores the digits
    Stack s = new Stack();
 
    // Stores the equivalent
    // string of integer N
    String strsum = Integer.toString(N);
 
    // Traverse the string
    for(int i = 0; i < strsum.length(); i++)
    {
         
        // Push the digit at i
        // in the stack
        s.push(strsum.charAt(i));
    }
 
    // While top element is '0'
    while (s.peek() == '0')
    {
         
        // Pop the top element
        s.pop();
    }
 
    // Stores the resultant number
    // without tailing 0's
    String res = "";
 
    // While s is not empty
    while (s.empty() == false)
    {
         
        // Append top element of S in res
        res = res + (char)s.peek();
 
        // Pop the top element of S
        s.pop();
    }
 
    StringBuilder str = new StringBuilder();
    str.append(res);
 
    // Reverse the string res
    str.reverse();
 
    return str.toString();
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Input
    int A = 130246, B = 450164;
 
    // Function Call
    System.out.println(removeTailing(A, B));
}
}
 
// This code is contributed by Dharanendra.L.V.


Python3
# Python 3 program for the above approach
 
# Function to remove trailing
# zeros from the sum of two numbers
def removeTailing(A,  B):
 
    # Stores the sum of A and B
    N = A + B
 
    # Stores the digits
    s = []
 
    # Stores the equivalent
    # string of integer N
    strsum = str(N)
 
    # Traverse the string
    for i in range(len(strsum)):
 
        # Push the digit at i
        # in the stack
        s.append(strsum[i])
 
    # While top element is '0'
    while (s[-1] == '0'):
 
        # Pop the top element
        s.pop()
 
    # Stores the resultant number
    # without tailing 0's
    res = ""
 
    # While s is not empty
    while (len(s) != 0):
 
        # Append top element of S in res
        res = res + (s[-1])
 
        # Pop the top element of S
        s.pop()
 
    # Reverse the string res
    res = list(res)
    res.reverse()
    res = ''.join(res)
 
    return res
 
 
# Driver Code
if __name__ == "__main__":
 
    # Input
    A = 130246
    B = 450164
 
    # Function Call
    print(removeTailing(A, B))
 
    # This code is contributed by ukasp.


Javascript


输出:
58041

时间复杂度: O(len(A + B))
辅助空间: O(len(A + B))