Example 1
- Input
- num1 = "2", num2 = "3"
- Output
- "6"
The only digit pair contributes units, so the canonical decimal result is "6".
Multiply two non-negative integers supplied as decimal strings and return the product as a string, without converting either input to an integer or using a big-integer library.
Given two non-negative integers num1 and num2 represented as decimal strings, return their product, also represented as a decimal string.
The inputs can hold up to 200 digits, far beyond what a fixed-width integer stores, so the digits must be combined by hand. Do not convert the whole strings to integers and do not use a big-integer library.
num1 and num2 consist of digits only.0 itself.The only digit pair contributes units, so the canonical decimal result is "6".
The shifted rows are 738, 6150 and 49200, whose sum is 56088. Several columns receive contributions, so their carries must be preserved.
Zero times any non-negative integer is zero. Return exactly "0", without extra leading zeros or an empty string.
The units row is 891 and the tens row is 8910. Their sum 9801 shows that a carry can propagate across overlapping partial products.
Decimal place value lets us multiply arbitrarily long inputs using only small digit arithmetic. In Example 1, the units digits 2 and 3 contribute 6 units. For a longer input, every digit pair contributes at its combined place value; add those contributions and carry each group of ten into the next column. Whole-number conversion is unnecessary.
Reveal Partial Products: intuition, complexity, and code
123 times 456: each digit of 456 produces its own shifted row before anything is added.i of num1 times a digit at index j of num2 always lands on the same answer positions: i + j and i + j + 1."0": return "0" at once instead of building an empty result.res[i + j] with plain assignment instead of +=, wiping out carries left by earlier pairs.0 digits at the front.Represent long decimal arithmetic by small digit contributions and their place values. Either add shifted rows or accumulate in a shared array; carry operations must preserve the represented value at every step.