Given two non-negative integers 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.
publicclass{ public String addStrings(String num1, String num2){ StringBuilder sBuilder = new StringBuilder();
int len1 = num1.length() - 1; int len2 = num2.length() - 1; int c = 0; for (; len1 >= 0 || len2 >= 0; len1--, len2--) {
int i = c; if (len1 >= 0) i += num1.charAt(len1) - '0'; if (len2 >= 0) i += num2.charAt(len2) - '0'; c = i / 10; i = i % 10; char a = (char) ('0' + i); sBuilder.append(a); }
近期评论