Easy understanding JavaScript solution


  • 0
    L
    /**
     * @param {string} num1
     * @param {string} num2
     * @return {string}
     */
    var addStrings = function(num1, num2) {
        num1 = num1.split('');
        num2 = num2.split('');
        var i = num1.length - 1;
        var j = num2.length - 1;
        var up = 0;
        var res = '';
        while(i >= 0 || j >= 0 || up > 0){
            var sum = up;
            if(i >= 0){
                sum += parseInt(num1[i]);
            }
            if(j >= 0){
                sum += parseInt(num2[j]);
            }
            if(sum >= 10){
                sum -= 10;
                up = 1;
            }else{
                up = 0;
            }
            res = sum + res;
            i --;
            j --;
        }
        return res;
    };
    

  • 0
    N

    This solution violates one of the constraints:

    You must not use any built-in BigInteger library or convert the inputs to integer directly.

    In other words, you cannot use parseInt


Log in to reply
 

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