Problem description:
43. String multiplication
Given two non-negative integers that are represented as strings,
num1
and num2
, return the product of num1
and num2
, which is also represented as a string.
Example 1:
Input: num1 = “2”, num2 = “3”
output: “6”
Example 2:
Input: num1 = “123”, num2 = “456”
output: “56088”
Description:
num1
and num2
have lengths less than 110. num1
and num2
only contain the Numbers 0-9
. num1
and num2
do not begin with zero, unless it is the number 0 itself. cannot be processed using any of the standard library’s large number types (such as BigInteger) or by converting the input directly to an integer.
Problem analysis:
So this isn’t a very difficult problem, but if you just do the regular multiplication, you can figure it out. Figure below:
The basic idea is as follows:
(1) can first flip the string, that is, start from the low order calculation.
(2) use an array to maintain the final result, updating it every time you multiply it, but pay attention to the carry case () pay attention to the two points, the same bit to add the carry, from low to high multiplication is carried. )
(3) finally, the 0
in front of the array is discarded and converted to string output.
Python3 implementation:
# @Time :2018/08/05
# @Author :LiuYinxing
# String Num
class Solution:
def multiply(self, num1, num2):
res = [0] * (len(num1) + len(num2)) # Initialization, array to hold the product.
pos = len(res) - 1
for n1 in reversed(num1):
tempPos = pos
for n2 in reversed(num2):
res[tempPos] += int(n1) * int(n2)
res[tempPos - 1] += res[tempPos] // 10 # Offset
res[tempPos] %= 10 # fractional remainder
tempPos -= 1
pos -= 1
st = 0
while st < len(res) - 1 and res[st] == 0: # How many zeros are in front of a statistic?
st += 1
return ''.join(map(str, res[st:])) # Remove the 0, then turn it into a string, and return
if __name__ == '__main__':
num1, num2 = "123", "456"
solu = Solution()
print(solu.multiply(num1, num2))
Welcome to correct me.
Read More:
- Python: How to Set Line breaks and tabs for Strings
- Python: leetcode 1672. Richest Customer Wealth
- Python time tuples are converted to timestamps, strings
- can‘t multiply sequence by non-int of type ‘numpy.float64‘
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- Python Error: pip install mysql-connector-python failed
- Linux installs Python and upgrades Python
- Opencv-python Install is Stuck Error: running setup.py bdist_wheel for opencv-python
- [Solved] supervisor Error: /usr/local/lib/python2.7/dist-packages/pkg_resources/py2_warn.py:22: UserWarning: Setuptools will stop working on Python 2
- npm install Error: stack Error: Can’t find Python executable “python”
- [Solved] Python Error: tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
- [Solved] cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\……
- Python: RNN principle realized by numpy
- Pychar can’t connect to Python console, but it can run. Py file, and Anaconda’s command line can run Python command
- Python TypeError: not all arguments converted during string formatting [Solved]
- Full explanation of SYS module of Python
- Python algorithm for “anagram” judgment problem