My Java Solution


  • 0
    R
    public String addStrings(String num1, String num2) {
            StringBuilder str = new StringBuilder();
            int carry = 0;
            for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
                int val1 = i >= 0 ? num1.charAt(i) - '0' : 0;
                int val2 = j >= 0 ? num2.charAt(j) - '0' : 0;
                int val = val1 + val2 + carry;
                str.insert(0, val % 10);
                carry = val / 10;
            }
            return str.toString();
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.