Java 9 lines solution


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

Log in to reply
 

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