Easy Java Solution


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

Log in to reply
 

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