class Solution {
public:
string addStrings(string num1, string num2) {
int jinwei=0;
if(num1.length()<num2.length())
{
string temp = num1;
num1 = num2;
num2 = temp;
}
int diff = num1.length() - num2.length();
num2.insert(0, diff,'0');
int indexOfNum1= (int)num1.length()-1;
string resultStr;
for(int i = (int)num2.length()-1;i>=0;i--)
{
char c2 = num2[i];
char c1 = num1[indexOfNum1];
int result = c2 -'0' + c1 - '0' +jinwei;
jinwei = 0;
char cr ='0';
if(result >=10)
{
cr +=result %10;
jinwei =1;
}
else
{
cr +=result;
}
resultStr.insert(0,1,(char)cr);
indexOfNum1 --;
}
if(jinwei == 1 )
{
resultStr.insert(0,1,'1');
}
return resultStr;
}
};
leetcode Add Strings
最新推荐文章于 2018-10-04 10:53:18 发布