没什么限定的话,先翻转,在一位一位加,记得进位就行了。。
public class Solution
{
public String addStrings(String num1, String num2)
{
StringBuilder sb = new StringBuilder(num1);
num1 = sb.reverse().toString();
sb = new StringBuilder(num2);
num2 = sb.reverse().toString();
if(num1.length() > num2.length())
{
String temp = num1;
num1 = num2;
num2 = temp;
}
String res = new String();
int carry = 0;
for(int i = 0; i < num1.length();i++)
{
int val = num1.charAt(i)+num2.charAt(i)- '0'-'0'+ carry;
if(val > 9) carry = 1;
else carry = 0;
val %= 10;
res+=Integer.toString(val);
}
for(int i = num1.length(); i < num2.length();i++)
{
int val = num2.charAt(i) - '0' + carry;
if(val > 9) carry = 1;
else carry = 0;
val %= 10;
res+= Integer.toString(val);
}
if(carry == 1) res += 1;
sb = new StringBuilder(res);
return sb.reverse().toString();
}
}