22ms Java solution


  • 0
    public String addStrings(String num1, String num2) {
        StringBuilder sb = new StringBuilder();
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        int add = 0;
        while(i > -1 || j > -1 || add != 0) {
            int sum = 0;
            if(i > -1) sum = num1.charAt(i--) - '0';
            if(j > -1) sum += num2.charAt(j--) - '0';
            sum += add;
            sb.append(sum % 10);
            add = sum / 10;
        }
        return sb.reverse().toString();
    }

Log in to reply
 

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