Simple Java solution


  • 0
    X
    public String addStrings(String num1, String num2) {
        final StringBuilder sb = new StringBuilder();
        int curry = 0;
        int sum = 0;
        
        for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >=0; i--, j--) {
            sum = curry + (i < 0 ? 0 : num1.charAt(i) - '0') + (j < 0 ? 0 : num2.charAt(j) - '0');
            sb.append(sum % 10);
            curry = sum / 10;
        }
        
        if (curry > 0) {
            sb.append(curry);
        }
        
        return sb.reverse().toString();
    }
    

Log in to reply
 

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