Given two non-negative integers
num1
and num2
represented as a string, return the sum of num1
and num2
.Example:
Input: A="123", B="234"
Output: "357"
Approach
Java
public class AddStrings {public static void main(String[] args) {String A = "123";String B = "234";System.out.println(addStrings(A, B));}// function to add two stringsstatic String addStrings(String num1, String num2) {// find the lenths of both stringsint n = num1.length(), m = num2.length();// if first is emptyif (n == 0)return num2;// if second is emptyif (m == 0)return num1;String res = "";int carry = 0;if (n == m) {for (int i = m - 1; i >= 0; i--) {int x = carry + num1.charAt(i) - '0' + num2.charAt(i) - '0';int y = x;x = x % 10;// convert into string and add it into the// resultres = x + res;carry = (y) / 10;}if (carry > 0) {int x = carry;// convert into string and add it into the// resultres = x + res;}} else if (n > m) {for (int i = m - 1; i >= 0; i--) {int x = carry + num1.charAt(n - m + i) - '0' +num2.charAt(i) - '0';int y = x;x = x % 10;// convert into string and add it into the// resultres = x + res;carry = (y) / 10;}for (int i = n - m - 1; i >= 0; i--) {int x = carry + num1.charAt(i) - '0';int y = x;x = x % 10;// convert into string and add it into the// resultres = x + res;carry = (y) / 10;}if (carry > 0) {int x = carry;// convert into string and add it into the// resultres = x + res;}} else {for (int i = n - 1; i >= 0; i--) {int x = carry + num1.charAt(i) - '0' +num2.charAt(m - n + i) - '0';int y = x;x = x % 10;// convert into string and add it into the// resultres = x + res;carry = (y) / 10;}for (int i = m - n - 1; i >= 0; i--) {int x = carry + num2.charAt(i) - '0';int y = x;x = x % 10;// convert into string and add it into the// resultres = x + res;carry = (y) / 10;}if (carry > 0) {int x = carry;// convert into string and add it into the// resultres = x + res;}}return res;}}
C++
#include <bits/stdc++.h>using namespace std;//function to add two stringsstring addStrings(string num1, string num2){//find the lenths of both stringsint n = num1.size(), m = num2.size();//if first is emptyif (n == 0)return num2;//if second is emptyif (m == 0)return num1;string res = "";int carry = 0;if (n == m){for (int i = m - 1; i >= 0; i--){int x = carry + num1[i] - '0' + num2[i] - '0';int y = x;x = x % 10;//convert into string and add it into the//resultres = to_string(x) + res;carry = (y) / 10;}if (carry > 0){int x = carry;//convert into string and add it into the//resultres = to_string(x) + res;}}else if (n > m){for (int i = m - 1; i >= 0; i--){int x = carry + num1[n - m + i] - '0' + num2[i] - '0';int y = x;x = x % 10;//convert into string and add it into the//resultres = to_string(x) + res;carry = (y) / 10;}for (int i = n - m - 1; i >= 0; i--){int x = carry + num1[i] - '0';int y = x;x = x % 10;//convert into string and add it into the//resultres = to_string(x) + res;carry = (y) / 10;}if (carry > 0){int x = carry;//convert into string and add it into the//resultres = to_string(x) + res;}}else{for (int i = n - 1; i >= 0; i--){int x = carry + num1[i] - '0' + num2[m - n + i] - '0';int y = x;x = x % 10;//convert into string and add it into the//resultres = to_string(x) + res;carry = (y) / 10;}for (int i = m - n - 1; i >= 0; i--){int x = carry + num2[i] - '0';int y = x;x = x % 10;//convert into string and add it into the//resultres = to_string(x) + res;carry = (y) / 10;}if (carry > 0){int x = carry;//convert into string and add it into the//resultres = to_string(x) + res;}}return res;}int main(){string A = "123";string B = "234";cout << addStrings(A, B);return 0;}
No comments:
Post a Comment