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();
    }
}

 

posted on 2022-04-09 05:55  阳光明媚的菲越  阅读(13)  评论(0编辑  收藏  举报