leetcode 415 两个字符串相加

string addstring(string s1,string s2)
{
    string ans="";
    int f=0;
    for(int i=s1.length()-1,j=s2.length()-1;i>=0||j>=0;i--,j--)
    {
        int x=i<0?0:s1[i]-'0';
        int y=j<0?0:s2[j]-'0';
        char c=(x+y+f)%10+'0';
        ans=c+ans;
        f=(x+y+f)/10;
    }
    if(f==1)
        ans='1'+ans;
    return ans;
}

猜你喜欢

转载自www.cnblogs.com/pengpenggege/p/9849129.html