给定两个数字N 1 和N 2由两个堆栈表示,以便它们的最高有效位出现在堆栈的底部,任务是计算并以堆栈的形式返回两个数字的和。
例子:
Input: N1={5, 8, 7, 4}, N2={2, 1, 3}
Output: {6, 0, 8, 7}
Explanation:
Step 1: Popped element from N1(= 4) + Popped element from N2(= 3) = {7} and rem=0.
Step 2: Popped element from N1(= 7) + Popped element from N2(= 1) = {7, 8} and rem=0.
Step 3: Popped element from N1(= 8) + Popped element from N2(= 2) = {7, 8, 0} and rem=1.
Step 4: Popped element from N1(= 5) = {7, 8, 0, 6}
On reverse the stack, the desired arrangement {6,0,8,7} is obtained.
Input: N1={6,4,9,5,7}, N2={213}
Output:{6, 5, 0, 0, 5}
方法:可以使用将链表表示的两个数字相加的概念来解决该问题。请按照以下步骤解决问题。
- 创建一个新的堆栈, res来存储两个堆栈的总和。
- 初始化变量rem和sum分别存储生成的进位和顶部元素的总和。
- 继续弹出两个堆栈的顶部元素,将总和%10推入res并将rem更新为sum / 10 。
- 重复上述步骤,直到纸堆为空。如果REM大于0,插入REM进栈。
- 反转res堆栈,使最高有效位出现在res堆栈的底部。
下面是结果方法的实现:
C++14
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to return the stack that
// contains the sum of two numbers
stack addStack(stack N1,
stack N2)
{
stack res;
int sum = 0, rem = 0;
while (!N1.empty() and !N2.empty()) {
// Calculate the sum of the top
// elements of both the stacks
sum = (rem + N1.top() + N2.top());
// Push the sum into the stack
res.push(sum % 10);
// Store the carry
rem = sum / 10;
// Pop the top elements
N1.pop();
N2.pop();
}
// If N1 is not empty
while (!N1.empty()) {
sum = (rem + N1.top());
res.push(sum % 10);
rem = sum / 10;
N1.pop();
}
// If N2 is not empty
while (!N2.empty()) {
sum = (rem + N2.top());
res.push(sum % 10);
rem = sum / 10;
N2.pop();
}
// If carry remains
while (rem > 0) {
res.push(rem);
rem /= 10;
}
// Reverse the stack.so that
// most significant digit is
// at the bottom of the stack
while (!res.empty()) {
N1.push(res.top());
res.pop();
}
res = N1;
return res;
}
// Function to display the
// resultamt stack
void display(stack& res)
{
int N = res.size();
string s = "";
while (!res.empty()) {
s = to_string(res.top()) + s;
res.pop();
}
cout << s << endl;
}
// Driver Code
int main()
{
stack N1;
N1.push(5);
N1.push(8);
N1.push(7);
N1.push(4);
stack N2;
N2.push(2);
N2.push(1);
N2.push(3);
stack res = addStack(N1, N2);
display(res);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.Stack;
class GFG{
// Function to return the stack that
// contains the sum of two numbers
static Stack addStack(Stack N1,
Stack N2)
{
Stack res = new Stack();
int sum = 0, rem = 0;
while (!N1.isEmpty() && !N2.isEmpty())
{
// Calculate the sum of the top
// elements of both the stacks
sum = (rem + N1.peek() + N2.peek());
// Push the sum into the stack
res.add(sum % 10);
// Store the carry
rem = sum / 10;
// Pop the top elements
N1.pop();
N2.pop();
}
// If N1 is not empty
while (!N1.isEmpty())
{
sum = (rem + N1.peek());
res.add(sum % 10);
rem = sum / 10;
N1.pop();
}
// If N2 is not empty
while (!N2.isEmpty())
{
sum = (rem + N2.peek());
res.add(sum % 10);
rem = sum / 10;
N2.pop();
}
// If carry remains
while (rem > 0)
{
res.add(rem);
rem /= 10;
}
// Reverse the stack.so that
// most significant digit is
// at the bottom of the stack
while (!res.isEmpty())
{
N1.add(res.peek());
res.pop();
}
res = N1;
return res;
}
// Function to display the
// resultamt stack
static void display(Stack res)
{
int N = res.size();
String s = "";
while (!res.isEmpty())
{
s = String.valueOf(res.peek()) + s;
res.pop();
}
System.out.print(s + "\n");
}
// Driver Code
public static void main(String[] args)
{
Stack N1 = new Stack();
N1.add(5);
N1.add(8);
N1.add(7);
N1.add(4);
Stack N2 = new Stack();
N2.add(2);
N2.add(1);
N2.add(3);
Stack res = addStack(N1, N2);
display(res);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to implement
# the above approach
# Function to return the stack that
# contains the sum of two numbers
def addStack(N1, N2):
res = []
s = 0
rem = 0
while (len(N1) != 0 and len(N2) != 0):
# Calculate the sum of the top
# elements of both the stacks
s = (rem + N1[-1] + N2[-1])
# Push the sum into the stack
res.append(s % 10)
# Store the carry
rem = s // 10
# Pop the top elements
N1.pop(-1)
N2.pop(-1)
# If N1 is not empty
while(len(N1) != 0):
s = rem + N1[-1]
res.append(s % 10)
rem = s // 10
N1.pop(-1)
# If N2 is not empty
while(len(N2) != 0):
s = rem + N2[-1]
res.append(s % 10)
rem = s // 10
N2.pop(-1)
# If carry remains
while(rem > 0):
res.append(rem)
rem //= 10
# Reverse the stack.so that
# most significant digit is
# at the bottom of the stack
res = res[::-1]
return res
# Function to display the
# resultamt stack
def display(res):
s = ""
for i in res:
s += str(i)
print(s)
# Driver Code
N1 = []
N1.append(5)
N1.append(8)
N1.append(7)
N1.append(4)
N2 = []
N2.append(2)
N2.append(1)
N2.append(3)
# Function call
res = addStack(N1, N2)
display(res)
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the stack that
// contains the sum of two numbers
static Stack PushStack(Stack N1,
Stack N2)
{
Stack res = new Stack();
int sum = 0, rem = 0;
while (N1.Count != 0 && N2.Count != 0)
{
// Calculate the sum of the top
// elements of both the stacks
sum = (rem + N1.Peek() + N2.Peek());
// Push the sum into the stack
res.Push((int)sum % 10);
// Store the carry
rem = sum / 10;
// Pop the top elements
N1.Pop();
N2.Pop();
}
// If N1 is not empty
while (N1.Count != 0)
{
sum = (rem + N1.Peek());
res.Push(sum % 10);
rem = sum / 10;
N1.Pop();
}
// If N2 is not empty
while (N2.Count != 0)
{
sum = (rem + N2.Peek());
res.Push(sum % 10);
rem = sum / 10;
N2.Pop();
}
// If carry remains
while (rem > 0)
{
res.Push(rem);
rem /= 10;
}
// Reverse the stack.so that
// most significant digit is
// at the bottom of the stack
while (res.Count != 0)
{
N1.Push(res.Peek());
res.Pop();
}
res = N1;
return res;
}
// Function to display the
// resultamt stack
static void display(Stack res)
{
int N = res.Count;
String s = "";
while (res.Count != 0)
{
s = String.Join("", res.Peek()) + s;
res.Pop();
}
Console.Write(s + "\n");
}
// Driver Code
public static void Main(String[] args)
{
Stack N1 = new Stack();
N1.Push(5);
N1.Push(8);
N1.Push(7);
N1.Push(4);
Stack N2 = new Stack();
N2.Push(2);
N2.Push(1);
N2.Push(3);
Stack res = PushStack(N1, N2);
display(res);
}
}
// This code is contributed by Amit Katiyar
6087
时间复杂度: O(N)
辅助空间: O(N)