GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #415 - Add Strings - LeetCode JavaScript Solutions

Back to all solutions

#415 - Add Strings

Easy • View on LeetCode • View on GitHub
String Math Simulation

Problem Description

Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.

You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.

Solution

/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var addStrings = function(num1, num2) {
  let result = '';

  for (let i = num1.length - 1, j = num2.length - 1, carry = 0; i > -1 || j > -1 || carry === 1;) {
    if (i > -1) {
      carry += num1.charCodeAt(i--) - 48;
    }
    if (j > -1) {
      carry += num2.charCodeAt(j--) - 48;
    }
    result = (carry % 10) + result;
    carry = Math.floor(carry / 10);
  }

  return result;
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.