LeetCode:415 字符串相加

class Solution {
    public String addStrings(String num1, String num2) {
        int mlen = Math.min(num1.length(),num2.length())-1;
        String res = new String();
        int a = num1.length()-1;
        int len1 = num1.length();
        int len2 = num2.length();
        int b = num2.length()-1;
        int t=0;
        while(mlen>=0){
            int x = Integer.parseInt(num1.substring(a,a+1)) + Integer.parseInt(num2.substring(b,b+1)) + t;
           
            a--;b--;
            t = x/10;
            x = x%10;
            res = Integer.toString(x) + res;
            
            mlen--;
        }
        while(a>=0){
            int x = Integer.parseInt(num1.substring(a,a+1)) + t;
            t = x/10;
            x = x%10;
            res = Integer.toString(x) + res;
            a--;
        }
        while(b>=0){
            int x = Integer.parseInt(num2.substring(b,b+1)) + t;
            t = x/10;
            x = x%10;
            res = Integer.toString(x) + res;
            b--;
        }
        if(t!=0){
            res = Integer.toString(t)+res;
        }
        return res;
    }
}

 

posted @ 2020-10-05 10:12  dlooooo  阅读(112)  评论(0编辑  收藏  举报