Easy Java Solution!


  • 0
    S
    public class Solution {
        public String addStrings(String num1, String num2) {
            StringBuilder sb = new StringBuilder();
            int n = num1.length(), m = num2.length();
            int i = n - 1, j = m - 1;
            int carry = 0;
            while (i >= 0 || j >= 0 || carry > 0) {
                int x = i > -1 ? Integer.parseInt("" + num1.charAt(i)) : 0;
                int y = j > -1 ? Integer.parseInt("" + num2.charAt(j)) : 0;
                int sum = x + y + carry;
                carry = sum / 10;
                sum = sum % 10;
                sb.append(sum);
                i--;
                j--;
            }
            return sb.reverse().toString();
        }
    }
    

Log in to reply
 

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