其他分享
首页 > 其他分享> > 415. Add Strings

415. Add Strings

作者:互联网

class Solution {
    public String addStrings(String num1, String num2) {
        StringBuilder res = new StringBuilder();
        int carry = 0;
        int i=num1.length()-1, j = num2.length()-1;
        while(i>=0 || j>=0|| carry>0){
            int x=0, y=0;
            if(i>=0)
                x = num1.charAt(i--)-'0';
            if(j>=0)
                y= num2.charAt(j--) -'0';
            int sum = x+y+carry;
            carry= sum/10;
            sum = sum%10;
            res.insert(0, (char)(sum+'0'));
        
        }
        return res.toString();
    }
}

 

标签:num1,num2,int,sum,415,Add,carry,Strings,String
来源: https://www.cnblogs.com/feiflytech/p/16120721.html