/*
415. Add Strings
Total Accepted: 4917
Total Submissions: 11391
Difficulty: Easy
Contributors: Admin
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
Show Company Tags
Hide Tags Math
*/
import java.util.*;
public class AddStrings {
public static void main(String[] args) {
AddStrings as = new AddStrings();
String num1 = "124";
String num2 = "86";
String res = as.addStrings(num1, num2);
System.out.println("res = " + res);
}
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int carry = 0;
int len1 = num1.length();
int len2 = num2.length();
for (int i = len1 - 1 , j = len2 - 1; i >=0 || j >= 0; i--, j--) {
int x = i < 0 ? 0 : num1.charAt(i) - '0';
int y = j < 0 ? 0 : num2.charAt(j) - '0';
sb.append((x + y + carry) % 10);
carry = (x + y + carry) / 10;
}
if (carry != 0) {
sb.append(carry);
}
return sb.reverse().toString(); // 421 + 68 -> 0 1 2 - > 2 1 0
}
}
415. Add Strings
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 问题: Given two non-negative numbers num1 and num2 represen...